我有一个布局
<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);
}
我希望传入的消息及其头像能够对齐right像 Viber 一样,外向left,但现在所有消息都是对齐的left
感谢所有建议
<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);
所以,我唯一改变的是你的LinearLayout
在 XML 文件中,更改layout_width
from fill_parent
to wrap_content
EDIT:(由于更多信息)
<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>
So, use
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
不要忘记依赖关系不能是圆形的
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)