1

背景が透明でいくつかのビューが中央に配置された相対レイアウトがあります。このレイアウトを拡張し、addView()を使用してメインレイアウトに追加します。拡張されたビューは、コーナーの中央の要素を親レイアウトに追加するだけです。追加されたビューをすべての画面に表示させることはできますか(fill_parentを試しましたが、無視されます)これは私が膨らませたビューです

<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>

これがビューを追加するメインレイアウトです

 <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 に答える