【问题标题】:addView FILL_PARENT not workingaddView FILL_PARENT 不起作用
【发布时间】:2012-07-20 07:21:09
【问题描述】:

我想通过 layoutInflater 将一个布局放入另一个布局中。

这是 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 文件,我已修复。

【问题讨论】:

    标签: android android-relativelayout layout-inflater


    【解决方案1】:

    使用这个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));
    }
    

    【讨论】:

    • 无论如何你仍然在错误地使用 addView(),请参阅 addView() 方法的文档。
    【解决方案2】:
    parent.addView(view, LayoutParams.FILL_PARENT);
    

    这个方法是 ViewGroup.addView(View child, int index) 实际调用的。 您需要调用 ViewGroup.addView(View child, ViewGroup.LayoutParams params) 方法来设置 LayoutParams。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-26
      • 2012-07-13
      • 1970-01-01
      相关资源
      最近更新 更多