2012-07-20 59 views
1

我想通過layoutInflater將佈局放入另一個佈局。addView FILL_PARENT不工作

這裏的main_layout.xml文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" > 

<ImageView 
    android:id="@+id/ads_image_view" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentTop="true" 
    android:src="@drawable/ic_launcher" /> 

<RelativeLayout 
    android:id="@+id/host_layout" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_above="@+id/navigation_buttons_layout" 
    android:layout_alignParentLeft="true" 
    android:layout_below="@+id/ads_image_view" > 
</RelativeLayout> 

<RelativeLayout 
    android:id="@+id/navigation_buttons_layout" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentRight="true" > 


    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentTop="true" 
     android:text="Button" /> 

</RelativeLayout> 

和這裏的vitrin_layout.xml文件:

<?xml version="1.0" encoding="utf-8"?> 
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="#ff0000" 
    android:text="smallred" /> 

終於onCreate方法爲我的主要活動:

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main_layout); 
    ViewGroup parent = (ViewGroup) findViewById(R.id.host_layout); 
    View view = LayoutInflater.from(getBaseContext()).inflate(
      R.layout.vitrin_layout, null); 
    parent.addView(view, LayoutParams.FILL_PARENT); 

    setTitle(getString(R.string.title_activity_vitrin)); 
} 

問題是,vitrin_layout,不適合其父(host_layout);儘管我已經設置了fill_parent,我可以!
我不明白爲什麼。

編輯:我真的很抱歉,但我貼了錯誤的vitrin_layout文件,我修好了。

回答

9

使用此addView方法。順便說一句,不要使用getBaseContext(),除非你知道你爲什麼使用它,而是使用活動的上下文(this)。

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main_layout); 
    ViewGroup parent = (ViewGroup) findViewById(R.id.host_layout); 
    View view = LayoutInflater.from(this).inflate(R.layout.vitrin_layout, null); 
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
     ViewGroup.LayoutParams.FILL_PARENT, 
     ViewGroup.LayoutParams.FILL_PARENT); 
    parent.addView(view, params); 

    setTitle(getString(R.string.title_activity_vitrin)); 
} 
+0

我在這個問題上犯了一個錯誤,請再次閱讀 – mehrmoudi 2012-07-20 07:39:22

+0

無論如何,你仍然在不經意地使用addView(),請參閱addView()方法的文檔。 – biegleux 2012-07-20 07:44:41

2
parent.addView(view, LayoutParams.FILL_PARENT); 

這種方法是ViewGroup.addView(查看孩子,INT指數)實際調用。 您需要調用ViewGroup.addView(View child,ViewGroup.LayoutParams params)方法來設置LayoutParams。

+0

我在這個問題上犯了一個錯誤,請再讀一遍 – mehrmoudi 2012-07-20 07:40:30