4

レイアウトがあります

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/background"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
    android:background="@drawable/chat_bg">

    <ImageView
        android:id="@+id/avatar"
        android:layout_width="32dip"
        android:layout_height="32dip"
        android:layout_marginRight="4dip"
        android:src="@drawable/avatar_1_1"        
        />

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="5sp"
        />

</LinearLayout>

そして私が持っているJavaコードで:

LayoutParams lp1 = (LayoutParams) textView.getLayoutParams();
    LayoutParams lp2 = (LayoutParams) avatarView.getLayoutParams();
    if (incoming) {
        lp1.gravity = Gravity.LEFT;
        lp2.gravity = Gravity.LEFT;
        textView.setBackgroundResource(R.drawable.speech_bubble_green);
        textView.setLayoutParams(lp1);
        avatarView.setLayoutParams(lp2);
    } else {
        lp1.gravity = Gravity.RIGHT;
        lp2.gravity = Gravity.RIGHT;
        textView.setBackgroundResource(R.drawable.speech_bubble_orange);
        textView.setLayoutParams(lp1);
        avatarView.setLayoutParams(lp2);
    }

受信メッセージとそのアバターがViber のように右に配置され、に送信されますが、すべてのメッセージは左に配置されます

すべての提案に感謝します

4

2 に答える 2

3
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/background"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        <!-- android:layout_gravity="left" -->
        android:orientation="horizontal">

        <ImageView
                android:id="@+id/avatar"
                android:layout_width="32dip"
                android:layout_height="32dip"
                android:layout_marginRight="4dip"
                android:src="@drawable/controller"
        />

        <TextView
                android:id="@+id/text"
                android:text="asd"
                android:textColor="#FFFFFF"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="5sp"
        />

</LinearLayout>

アクティビティで重力を設定するには:

LinearLayout lp = (LinearLayout) findViewById(R.id.background);
lp.setGravity(Gravity.RIGHT);
lp.setGravity(Gravity.Left);

だから、私が変更したのはあなたのin XMLLinearLayoutファイルだけです。layout_widthfill_parentwrap_content


編集:(詳細情報のため)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/background"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:layout_gravity="left"
                android:orientation="horizontal">


    <RelativeLayout android:id="@+id/avatar2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@id/text2">
        <ImageView
                android:id="@+id/avatar"
                android:layout_width="32dip"
                android:layout_height="32dip"
                android:layout_marginRight="4dip"
                android:src="@drawable/controller"
                />
    </RelativeLayout>

    <RelativeLayout android:id="@+id/text2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toLeftOf="@id/avatar">
        <TextView
                android:id="@+id/text"
                android:text="asd"
                android:textColor="#FFFFFF"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
    </RelativeLayout>

</RelativeLayout>

だから、使う

RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)yourLayout.getLayoutParams();
params.addRule(RelativeLayout.RIGHT_OF, R.id.id_to_be_left_of);
//params.addRule(RelativeLayout.LEFT_OF, R.id.id_to_be_left_of);
yourLayout.setLayoutParams(params); //causes layout update

依存関係は循環できないことを忘れないでください

于 2013-08-08T09:44:35.240 に答える
1

解決策はこちら

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/background"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingRight="4dip"
android:paddingBottom="4dip"
android:background="@drawable/chat_bg">

<ImageView
    android:id="@+id/avatar"
    android:layout_width="32dip"
    android:layout_height="32dip"
    android:layout_marginLeft="4dip"
    android:src="@drawable/avatar_1_1"
    />

<TextView
    android:id="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@id/avatar"
    android:paddingLeft="4dip"
    />

そしてここにJavaコード:

RelativeLayout bg = (RelativeLayout)view.findViewById(R.id.background);         
        if (incoming) {
            textView.setBackgroundResource(R.drawable.speech_bubble_orange);

            bg.setGravity(Gravity.LEFT);
        } else {
            textView.setBackgroundResource(R.drawable.speech_bubble_green);

            LayoutParams avatarParams = (LayoutParams) avatarView.getLayoutParams();
            avatarParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
            avatarView.setLayoutParams(avatarParams);

            /*android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_toRightOf="@id/avatar"
                    android:paddingLeft="4dip"*/
            LayoutParams tvParam = (LayoutParams) textView.getLayoutParams();
            tvParam.removeRule(RelativeLayout.RIGHT_OF);
            tvParam.addRule(RelativeLayout.LEFT_OF, R.id.avatar);
            textView.setLayoutParams(tvParam);

            bg.setGravity(Gravity.RIGHT);
        }
于 2013-08-15T03:23:49.390 に答える