android关于获取布局宽高的问题

通过定时器和handler的结合是可以拿到main.xml(也就是当前Activity显示的布局)中布局的宽高。但是如果main.xml中的布局含有include或者 ViewStub导入的其他xml布局。那么我们怎么在当前Activity用代码来获取include或者ViewStub中xml的布局的宽高呢。下面举例说明:
main.xml文件代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:gravity="center">

<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/bk"
android:layout_gravity="center"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp"
android:layout_marginTop="50dp"
android:layout_marginBottom="70dp"
android:padding="4dp">

<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">

< ViewStub
android:id="@+id/viewStub_2"
android:layout="@layout/two_linearlayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>


</RelativeLayout>

</FrameLayout>

<Button
android:id="@+id/btn_changeLayout"
android:text="改变布局"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true">
</Button>

</RelativeLayout>
引入的 two_linearlayout.xml代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">


<LinearLayout
android:id="@+id/ ll_viewArea1"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="150dp"
android:background="#ffffff"
android:layout_margin="4dp">
</LinearLayout>

<LinearLayout
android:id="@+id/ ll_viewArea2"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="200dp"
android:layout_below="@+id/ll_viewArea1"
android:background="#ffffff"
android:layout_margin="4dp">
</LinearLayout>


</LinearLayout>
现在要在主Activity(也就是加载main.xml文件的Activity)中获取 two_linearlayout.xml文件中的 ll_viewArea1和ll_viewArea2两个线性布局的宽高。代码如下:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
viewStub_2 = (ViewStub) findViewById(R.id.viewStub_2);
btn_change_Layout = (Button) findViewById(R.id.btn_changeLayout);

parm = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);
// 一定要先显示ViewStub_2,才能获取它所引入的xml中控件的宽高
viewStub_2.inflate();
注意这里:如果通过引入 two_linearlayout.xml文件,然后获取它内部的控件,这样是无法获取控件的宽高的( 但是可以获取到控件id
// View layout = getLayoutInflater().inflate(R.layout.two_linearlayout, null);
// ll_viewArea1 = (LinearLayout) layout .findViewById(R.id.ll_viewArea1);
// ll_viewArea2 = (LinearLayout) layout .findViewById(R.id.ll_viewArea2);


//通过this获取当前布局文件中的控件宽高(因为通过viewStub标签将 two_linearlayout.xml引入到了main.xml中,所以 two_linearlayout.xml中的控件属于main.xml的控件,因此用this也可以获取)
ll_viewArea1 = (LinearLayout) this.findViewById(R.id.ll_viewArea1);
ll_viewArea2 = (LinearLayout) this.findViewById(R.id.ll_viewArea2);

final Handler myHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
if(msg.what == 1) {
if(ll_viewArea1!=null&&ll_viewArea1.getWidth()!=0) {
viewArea1 = new ViewArea(MainActivity.this,R.drawable.psu,ll_viewArea1);
ll_viewArea1.addView(viewArea1,parm);
}
if(ll_viewArea2!=null&&ll_viewArea2.getWidth()!=0) {
viewArea2 = new ViewArea(MainActivity.this,R.drawable.img2,ll_viewArea2);
ll_viewArea2.addView(viewArea2,parm);
timer.cancel();
}
}
}
};

timer = new Timer();
TimerTask task = new TimerTask(){
public void run() {
Message message = new Message();
message.what = 1;
myHandler.sendMessage(message);
}
};
//延迟每次延迟10 毫秒 隔1秒执行一次
timer.schedule(task,10,1000);

}
这样就可以获取到另一个xml文件中控件的宽高了!
转帖请注明原博客地址: http://blog.163.com/zhaolin53636848@126/以及 博主姓名:木木
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值