<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>
To establish gravity in your work:
LinearLayout lp = (LinearLayout) findViewById(R.id.background); lp.setGravity(Gravity.RIGHT); lp.setGravity(Gravity.Left);
So the only thing I changed is in your LinearLayout in the XML file, change layout_width from fill_parent to wrap_content
EDIT: (due to more info)
<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);
Remember that dependencies cannot be round.
source share