1

I have a relative layout with transparent background and a some views arranged in the middle, i inflate this layout and add it to the main layout with addView(), the inflated view just adds the middle elements in the corner to the parent layout, how can i make the added view occupy all the screen (tried fill_parent but it just gets ignored) This is the view i inflate

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

    <RelativeLayout
        android:id="@+id/layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:background="@color/info_bar_bg_color" >

        <ImageView
            android:id="@+id/channel_pic"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true" />

        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_toRightOf="@id/channel_pic"
            android:ellipsize="end"
            android:maxLines="2"
            android:textColor="@color/title_color"
            android:textSize="@dimen/title_text_size" />

        <TextView
            android:id="@+id/date"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/title"
            android:ellipsize="end"
            android:maxLines="1"
            android:textColor="@color/date_color"
            android:textSize="@dimen/date_text_size" />

        <TextView
            android:id="@+id/hourStart"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/date"
            android:textColor="@color/title_color"
            android:textSize="@dimen/title_text_size" />

        <TextView
            android:id="@+id/hourEnd"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/hourStart"
            android:textColor="@color/title_color"
            android:textSize="@dimen/title_text_size" />

        <ImageView
            android:id="@+id/share_pic"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:src="@drawable/epgshare_0" />

        <ImageView
            android:id="@+id/bell_pic"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_below="@id/share_pic"
            android:src="@drawable/epgsharebell_0" />
    </RelativeLayout>

</RelativeLayout>

and this is the main layout i add the view to

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/ic_launcher"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:onClick="openInfoBar"
        android:text="Open InfoBar" />

</RelativeLayout>
4

2 回答 2

2

我膨胀这个布局并使用 addView() 将其添加到主布局中,膨胀的视图只是将角落的中间元素添加到父布局中,

发生这种情况(很可能)是因为您没有LayoutParams为膨胀的布局视图设置正确的。你可能会使用这样的东西:

View v = inflater.inflate(R.layout.to_fill_parent, null);

结合简单的addView(v)方法将使视图表现得像你看到的那样。正确的方法是:

View v = inflater.inflate(R.layout.to_fill_parent, (ViewGroup)findViewById(R.id.layout), false);
//add the view with addView
于 2013-01-26T16:56:43.873 回答
0

更改:

<RelativeLayout
    android:id="@+id/layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:background="@color/info_bar_bg_color" >

如 :

android:layout_width="500dp"
android:layout_height="600dp"

您必须尝试其他数字,而不是 500 和 600 ,,,,当您拥有最佳尺寸时保存它^_^

于 2013-01-26T17:44:15.347 回答