如何在RelativeLayout的编程设置重力 [英] How to set gravity in programmatically in a relativelayout
问题描述
如何设置programmingcally重力一个RelativeLayout的。我有一个名字XML布局的 chat_viewer_message.xml 如下:
How to set gravity in programmingcally in a relativelayout. I have a XML layout with name chat_viewer_message.xml as below:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/background"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="4dip"
android:paddingBottom="4dip"
android:gravity="left"
android:background="@drawable/chat_bg_in">
<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"
/>
</RelativeLayout>
和编程code示意图如下:
And code view in programming is as below:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final int type = getItemViewType(position);
final View view;
if (convertView == null) {
final int resource = R.layout.chat_viewer_message;
view = activity.getLayoutInflater()
.inflate(resource, parent, false);
if (type == TYPE_MESSAGE)
((TextView) view.findViewById(R.id.text)).setTextAppearance(
activity, appearanceStyle);
} else
view = convertView;
final MessageItem messageItem = (MessageItem) getItem(position);
String name;
final String account = messageItem.getChat().getAccount();
final String user = messageItem.getChat().getUser();
final String resource = messageItem.getResource();
final boolean incoming = messageItem.isIncoming();
final String server_host = view.getResources().getString(
R.string.server_host);
if (isMUC) {
name = resource;
}
if (incoming) {
view.setBackgroundResource(R.drawable.chat_bg_in);
} else {
// I WANT TO GRAVITY TO RIGHT. HOW TO CODE? THANKS
view.setBackgroundResource(R.drawable.chat_bg_out);
}
return view;
}
在id为背景的RelativeLayout见重力,默认重力离开了,所以我想,如果!传入在RelativeLayout的价值重心是正确的,而不是离开了。
See gravity at the relativelayout with id = background, the default gravity is LEFT, So I want to if !incoming the value gravity at relativelayout be RIGHT instead LEFT.
感谢。
推荐答案
演员视图
来容器布局,在这种情况下 RelativeLayout的
,并使用 setGravity(INT)
就可以了:
Cast view
to container layout, in this case RelativeLayout
, and use setGravity(int)
on it:
if (incoming) {
view.setBackgroundResource(R.drawable.chat_bg_in);
} else {
// I WANT TO GRAVITY TO RIGHT. HOW TO CODE? THANKS
view.setBackgroundResource(R.drawable.chat_bg_out);
((RelativeLayout) view).setGravity(Gravity.RIGHT);
}
一个忠告(从文档):
A word of caution (from the docs):
请注意,由于RelativeLayout的考虑每个孩子的定位
相对于彼此是显著,设置重力会影响
所有儿童的定位作为母体内的单个单元。
发生这种情况后,孩子一直相对定位。
Note that since RelativeLayout considers the positioning of each child relative to one another to be significant, setting gravity will affect the positioning of all children as a single unit within the parent. This happens after children have been relatively positioned.
这篇关于如何在RelativeLayout的编程设置重力的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!