1

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>
Neha Shukla
  • 3,572
  • 5
  • 38
  • 69
Chuan Liu
  • 25
  • 1
  • 6

6 Answers6

1

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>
suitianshi
  • 3,300
  • 1
  • 17
  • 34
  • instead `0px` it shouldn't be `0dp` [Difference between px, dp, dip and sp in Android?](http://stackoverflow.com/questions/2025282/difference-between-px-dp-dip-and-sp-in-android) – Kaushik Sep 02 '14 at 09:02
  • for 0, they're all the same. – suitianshi Sep 02 '14 at 09:05
1

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>
Yeray
  • 1,265
  • 1
  • 11
  • 23
0

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));
Sidd
  • 172
  • 9
0

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

shashi2459
  • 581
  • 1
  • 6
  • 15
0

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

marcel
  • 313
  • 4
  • 10
0

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

MSA
  • 2,502
  • 2
  • 22
  • 35