android关于获取布局宽高的问题
2012-07-06 15:48
435 查看
通过定时器和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/以及 博主姓名:木木
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/以及 博主姓名:木木
相关文章推荐
- Unity3D-关于使用LayoutGroup自动布局后获取宽高不对的问题
- Android中软键盘弹出时关于布局的问题
- Android获取屏幕宽高要注意的问题
- 关于Android发送短信获取送达报告的问题(推荐)
- Android开发之--关于获取屏幕宽度和高度的问题
- Android中关于ImageView网络获取的图片的缩放问题
- android关于EditText获取焦点的一个问题
- android新手关于左右滑动的问题,布局把<android.support.v4.view.ViewPager/><ImageView/> 放在上面就不行了。
- 关于java 和 android 下获取指定时间是本年第几周不一致问题的解决方法
- Android学习问题:关于AlertDialog中自定义布局带有的EditText无法弹出键盘
- android新手关于左右滑动的问题,布局把<android.support.v4.view.ViewPager/><ImageView/> 放在上面就不行了。
- Android 关于显示键盘,布局错乱网上顶的问题
- 关于获取视图自动布局后的尺寸问题
- 关于Android下Delphi XE7获取通讯录的问题
- Android 关于显示键盘,布局错乱网上顶的问题
- 关于获取设备的唯一设备ID,MAC地址 IOS/Android问题
- Android中软键盘弹出时关于布局的问题
- Android中关于字体宽高的获取
- android获取view宽高的几种方法,解决获取高宽为0问题
- 关于Android布局中键盘遮挡问题