If I want to make my TextView width exactly equal to the width of its parent, I can use
android:layout_width="fill_parent"
What about if I want to make it half the width of the part? Or, in general, set the width relative to the parent's width?
Edit: I am using Relative Layout
My screen looks like this.
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" />
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.
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>
First of all don't use "fill_parent" it a deprecated tearm, use "match_parent".
Secondly where do you want to place the half parent TextView
in the parent?
As you are using RelativeLayout this action is a little harder, try to do it from code, some thing like this:
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int parentHeight = MeasureSpec.getSize(heightMeasureSpec);
int myWidth = (int) (parentHeight * 0.5);
super.onMeasure(MeasureSpec.makeMeasureSpec(myWidth, MeasureSpec.EXACTLY),
heightMeasureSpec);
}
reference: How to size an Android view based on its parent's dimensions
If you want to use RelativeLayout, then best approach is to put a dummy view at the center of the screen and put the first view to the right and second to the left hand side of the dummy view 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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_contentt"
android:background="#ff0000"
android:layout_toLeftOf="@id/dummyView/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ff0000"
android:layout_toRightOf="@id/dummyView"/>
<View
android:id="+@id/dummyView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="invisible"
android:layout_centerInParent="true"/>
</RelativeLayout>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With