Chuan Liu
Chuan Liu

Reputation: 25

Android how to set the width of a view to a certain ratio of the width of the screen?

I create 3 TextViews placed horizontally in a LinearLayout. Now, I want them to averagely take up 1/3 of the width of screen respectively. In another word, the width of any TextView is determined by the width of the screen and the ratio is always 1/3.

I wonder if there's any way to achieve this target with only modifying the xml file?

main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView1" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView2" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView3" />

</LinearLayout>

Upvotes: 1

Views: 145

Answers (6)

MSA
MSA

Reputation: 2482

You only need to add one code line on each textview

android:layout_weight="0.3"

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView1"
    android:gravity="center_horizontal"
    android:layout_weight="0.3"/>

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView2"
    android:gravity="center_horizontal"
    android:layout_weight="0.3"/>

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView3"
    android:gravity="center_horizontal"
    android:layout_weight="0.3"/>

</LinearLayout>

enter image description here

Upvotes: 0

marcel
marcel

Reputation: 313

add a extra linear layout on the textview. Set the width of linear layout like android:layout_width = "0px" and the text via toandroid:layout_width="full_parent"

Do not forget to make on the parent linear layout to set a weigthsum and layout_weigth in childs

Upvotes: 0

shashi2459
shashi2459

Reputation: 581

apply weight android:layout_weight="?" to every view in your xml file.

Upvotes: 0

Sidd
Sidd

Reputation: 172

You should did it programmatically :

TypedValue tv = new TypedValue();
TypedValue.complexToDimensionPixelSize(tv.data,activity.getResources().getDisplayMetrics());

Or with that :

DisplayMetrics displayMetrics = activity.getResources().getDisplayMetrics();
int px = Math.round(dp * (displayMetrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT));

Upvotes: 0

Yeray
Yeray

Reputation: 1265

Try this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:weightSum="3" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="TextView1" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="TextView2" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="TextView3" />

</LinearLayout>

Upvotes: 1

suitianshi
suitianshi

Reputation: 3340

Try this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="0px"
        android:layout_height="wrap_content"
        android:text="TextView1"
        android:layout_weight="1" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="0px"
        android:layout_height="wrap_content"
        android:text="TextView2"
        android:layout_weight="1" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="0px"
        android:layout_height="wrap_content"
        android:text="TextView3"
        android:layout_weight="1" />

</LinearLayout>

Upvotes: 1

Related Questions