我试图以编程方式将线性布局对齐到另一个线性布局中,但setGravity(Gravity.RIGHT)
不起作用。
我知道gravity
和layout_gravity
之间的差异。
这是xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:id="@+id/initial_wrapper"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<LinearLayout
android:id="@+id/conversationwrapper"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/bubble_yellow"
>
<TextView
android:id="@+id/messagecontent_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="5dip"
android:paddingLeft="10dip"
android:text="Message_content"
android:textSize="15dp" />
<TextView
android:id="@+id/timestamp_tv"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:text="HH:MM"
android:ems="2"
android:textSize="12dp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
我的适配器中的getView()代码:
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
if (row == null) {
LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.listitem_conversation, parent, false);
}
wrapper = (LinearLayout) row.findViewById(R.id.conversationwrapper);
initial_wrapper = (LinearLayout) row.findViewById(R.id.initial_wrapper);
time = (TextView) row.findViewById(R.id.timestamp_tv);
Message coment = getItem(position);
content = (TextView)row.findViewById(R.id.messagecontent_tv);
content.setText(coment.content);
time.setText(getCurrentTimeStamp());
wrapper.setBackgroundResource(!coment.mine ? R.drawable.bubble_yellow : R.drawable.bubble_green);
initial_wrapper.setGravity(!coment.mine ? Gravity.LEFT : Gravity.RIGHT);
return row;
}
但它总是碰巧在左边,即使图像是绿色的。
奇怪的是,如果我输入
android:gravity="right"
在initial_wrapper中,它可以在预览中使用,但不能在设备中使用。
我正在使用Moto G和Nexus 5,但它们都不起作用
答案 0 :(得分:0)
问题可以在这里:android:layout_width="fill_parent"
,你如何在这里设置android:gravity="right"
,如果你的布局填满整个父母?你应该尝试使用
<LinearLayout
android:id="@+id/initial_wrapper"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
而不是:
<LinearLayout
android:id="@+id/initial_wrapper"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
答案 1 :(得分:0)
这样做
r
在<LinearLayout
android:id="@+id/initial_wrapper"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity = "left"
>
执行此操作
getView()
希望有所帮助