How to make my TextView width half of its parent?
The quick and dirty way is like this:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:weightSum="2"> <TextView android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:background="#ff0000" /> </LinearLayout></RelativeLayout>
You have to wrap it in another container, then use only half the weight sum of the parent.
Use android:layout_weight="0.5"
it will work for linearLayout
<LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:weightSum="1" android:gravity="center" android:orientation="vertical" > <TextView android:id="@+id/feedback" android:layout_width="wrap_content" android:layout_weight="0.5" android:textSize="15pt" />
You may want to set the weightSum of the parent LinearLayout
, something like this:-
<LinearLayout android:id="@+id/linearLayout1" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:gravity="center_horizontal" android:padding="6dp" android:weightSum="2"> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="match_parent" android:text="@string/hello" android:layout_weight="1" /></LinearLayout>