在Android中从Java更改RelativeLayout
我有一个XML RelativeLayout在Android中从Java更改RelativeLayout,android,Android,我有一个XML RelativeLayout <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <RelativeLayout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/relativeBackGround"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_gravity="right"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp" >
<ImageView
android:id="@+id/img1"
android:layout_width="40dp"
android:layout_height="40dp"
android:src="@drawable/sample" />
<LinearLayout
android:id="@+id/linout"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/img1"
android:background="@drawable/bubble_left" >
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="text1" />
<TextView
android:id="@+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:text="text2"
android:textSize="10sp" />
</LinearLayout>
</RelativeLayout>
</LinearLayout>
setGravity不起作用,我不知道如何对齐,你能帮我吗?你可以用类似于以下的东西设置LinearLayout的布局:
LinearLayout ll = (LinearLayout) view.findViewById(R.id.linout);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
ll.setLayoutParams(lp);
然后根据是奇数行还是偶数行切换规则
LinearLayout ll = (LinearLayout) view.findViewById(R.id.linout);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
ll.setLayoutParams(lp);