本文介绍了addView FILL_PARENT不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提出一个布局到通过layoutInflater另一个。

I want to put a layout into another one via layoutInflater.

这里的main_layout.xml文件:

Here's the main_layout.xml file:

<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文件:

and here's the vitrin_layout.xml file:

<?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方法:

and finally the onCreate method for my main activity:

@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永远在那里我能!

我想不通为什么。

The problem is that the vitrin_layout, does not fit into its parent(host_layout); although I've set fill_parent where ever I could!
I can't figure out why.

编辑:我真的很抱歉,但我错了粘贴文件vitrin_layout,我固定它

Edited: I'm really sorry, but i'd pasted wrong vitrin_layout file, I fixed it.

推荐答案

使用此addView方法。顺便说一句不使用 getBaseContext(),除非你知道你为什么使用它,而是使用一个活动的背景下(这个)。

Use this addView method. By the way don't use getBaseContext() unless you know why you are using it, instead use context of an activity (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 FILL_PARENT不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 06:33