LinearLayout加载动态view时显示不全解决办法

问题:在某个主要Layout里面有个LinearLayout,用来动态加载别的view页面,若是须要加载的动态view内容很空洞(例如没有足够的Text长度来撑起整个layout宽度),这时候这个被加载的view就会不能以fill_parent的width来显示。html

如图: java



实际想要的效果为:
android

java code:
ide

[java]  view plain copy
  1. public class TestActivity extends Activity {  
  2.     /** Called when the activity is first created. */  
  3.     @Override  
  4.     public void onCreate(Bundle savedInstanceState) {  
  5.         super.onCreate(savedInstanceState);  
  6.         setContentView(R.layout.main);  
  7.   
  8.         LinearLayout layout = (LinearLayout) findViewById(R.id.layout);  
  9.         View view = getLayoutInflater().inflate(R.layout.test, null);  
  10.         layout.addView(view);  
  11.     }  
  12. }  

main 布局:布局

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:background="#fff"  
  6.     android:orientation="vertical" >  
  7.   
  8.     <LinearLayout  
  9.         android:id="@+id/layout"  
  10.         android:layout_width="fill_parent"  
  11.         android:layout_height="fill_parent" >  
  12.     </LinearLayout>  
  13.   
  14. </LinearLayout>  


test 布局:

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:background="#fff"  
  6.     android:orientation="vertical" >  
  7.   
  8.     <TextView  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:text="测试页面" />  
  12.   
  13.     <LinearLayout  
  14.         android:layout_width="fill_parent"  
  15.         android:layout_height="wrap_content"  
  16.         android:layout_margin="5dip"  
  17.         android:orientation="horizontal" >  
  18.   
  19.         <TextView  
  20.             android:layout_width="wrap_content"  
  21.             android:layout_height="wrap_content"  
  22.             android:text="帐号:" />  
  23.   
  24.         <EditText  
  25.             android:layout_width="fill_parent"  
  26.             android:layout_height="wrap_content" />  
  27.     </LinearLayout>  
  28.   
  29.     <LinearLayout  
  30.         android:layout_width="fill_parent"  
  31.         android:layout_height="wrap_content"  
  32.         android:layout_margin="5dip"  
  33.         android:orientation="horizontal" >  
  34.   
  35.         <TextView  
  36.             android:layout_width="wrap_content"  
  37.             android:layout_height="wrap_content"  
  38.             android:text="密码:" />  
  39.   
  40.         <EditText  
  41.             android:layout_width="fill_parent"  
  42.             android:layout_height="wrap_content" />  
  43.     </LinearLayout>  
  44.   
  45. </LinearLayout>  

解决办法:

在动态view的添加代码前定义一个LayoutParams,而后在添加view时明肯定义一下这个view的LayoutParams测试

[html]  view plain copy
  1. public class TestActivity extends Activity {  
  2.     /** Called when the activity is first created. */  
  3.     @Override  
  4.     public void onCreate(Bundle savedInstanceState) {  
  5.         super.onCreate(savedInstanceState);  
  6.         setContentView(R.layout.main);  
  7.   
  8.         LinearLayout layout = (LinearLayout) findViewById(R.id.layout);  
  9.         View view = getLayoutInflater().inflate(R.layout.test, null);  
  10.         LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);  
  11.         layout.addView(view, params);  
  12.     }  
  13. }