0

したがって、私のテキストビューは画像ビューの上に描画する必要があるため、xml で次のように定義されます。

    <ImageView
        android:id="@+id/chatBalloon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_marginLeft="-5dp"
        android:layout_marginRight="-5dp"
        android:layout_marginTop="2dp"
        android:layout_toRightOf="@+id/chatItemProfPic"
        android:scaleType="fitXY"
        android:src="@drawable/chat_bar_user" />

    <TextView
        android:id="@+id/userText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="7dp"
        android:layout_marginTop="3dp"
        android:layout_toRightOf="@+id/chatItemProfPic"
        android:text="username"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textSize="15sp" />

しかし、textView には複数行のテキストを含めることができるため、それに応じて imageView の高さを大きくする必要があります。これは、次のルールを追加することで実現できます。

android:layout_alignBottom="idOfText"

しかし、その部分で textView が定義されていないため、アプリがクラッシュします。ビューが描画される前にonCreateで呼び出すため、LayoutParamsのaddRuleでコードから実行しようとすると同じ結果になります。

これをバイパスする方法はありますか?

解決済み: 最終的な xml:

    <ImageView
        android:id="@+id/chatBalloon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_marginLeft="-5dp"
        android:layout_marginRight="-5dp"
        android:layout_marginTop="2dp"
        android:layout_toRightOf="@+id/chatItemProfPic"
        android:scaleType="fitXY"
        android:layout_alignBottom="@+id/userText"
        android:src="@drawable/chat_bar_user" />

    <TextView
        android:id="@id/userText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="7dp"
        android:layout_marginTop="3dp"
        android:layout_toRightOf="@+id/chatItemProfPic"
        android:text="username"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textSize="15sp" />
4

2 に答える 2

2

あなたが使う

 android:layout_alignBottom="@+id/idOfText"

特定の ID を最初に参照するとき。リソース識別子のリストに id を追加する「+」に注意してください。

次に、次のように、「+」を使用せずに既存の ID をウィジェットに割り当てることができます。

android:id="@id/idOfText"

後。ID を割り当てるときに ID を作成するのが一般的です。そのため、相対レイアウトで "+" の存在を気にする必要があるだけです。

あなたの特定のケースでは:

<ImageView
    android:id="@+id/chatBalloon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_marginLeft="-5dp"
    android:layout_marginRight="-5dp"
    android:layout_marginTop="2dp"
    android:layout_toRightOf="@id/chatItemProfPic"
    android:layout_alignBottom="@+id/userText"
    android:scaleType="fitXY"
    android:src="@drawable/chat_bar_user" />

<TextView
    android:id="@id/userText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="7dp"
    android:layout_marginTop="3dp"
    android:layout_toRightOf="@id/chatItemProfPic"
    android:text="username"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textSize="15sp" />

android:id = "@+id/chatItemProfPic"ウィジェットへの参照の前にウィジェットを宣言していると仮定すると、次を使用してプロファイル写真ウィジェットの ID を設定する必要があります。それ以外の場合は、同様に、最初の参照に「+」を使用し、「+」なしでウィジェットを宣言するときに ID を割り当てます。

于 2013-08-23T11:14:32.460 に答える
0

その画像をテキストビューの背景のように入れないのはなぜですか。

または次のようなもの:

<RelativeLayout
            android:id="@+id/Rlayout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="35dp" >

            <ImageView
                android:id="@+id/image"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:src="@drawable/img" />

            <TextView
                android:id="@+id/text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true"
                android:text="lorem ipsum"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:textSize="@dimen/dimension" />
        </RelativeLayout>

相対的なレイアウトでは、テキストをイメージビューのすぐ上に簡単に配置できます

于 2013-08-23T11:17:59.763 に答える