Create a TextView with equal width in row

I am trying to create a layout with three text fields next to each other. Textures have different lengths.

All I want to do is arrange them so that they always have equal layout width regardless of the length of the text.

The approaches I tried:

1) I tried using linear_layout and set the weight of the text views to 1, but this does not, because it only applies to the remaining width after placing all the text elements.

2) I also tried using the table layout and stretching all the columns using stretchColumns = "*". but also selects the required width in the length of the texts and only then stretches the columns.

3) It’s a hack, but it doesn’t work either. I tried to have a linear layout inside a relative layout with three text images so as not to have text, but made them have equal width by setting their weights. This works, but then I want my next views to be in relative layout to align myself to the right and left edges of the views in a linear layout. But that does not work. Here is my layout that I used for this.

        <RelativeLayout
            android:layout_width="fill_parent" android:layout_height="fill_parent" >
            <!-- This is a hack to have all three textviews in a row to obtain equal width -->
            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >
                <TextView android:id="@+id/tv_ref1"
                    android:layout_width="0dp" android:layout_height="wrap_content"
                    android:text="My"
                    android:layout_weight="1.0"
                    android:gravity="center" />
                <TextView android:id="@+id/tv_ref2"
                    android:layout_width="0dp" android:layout_height="wrap_content"
                    android:text="My"
                    android:layout_weight="1.0"
                    android:gravity="center" />
                <TextView android:id="@+id/tv_ref3"
                    android:layout_width="0dp" android:layout_height="wrap_content"
                    android:text="My ghghhfghjjghjkkhgfhfghfjhjh"
                    android:layout_weight="1.0"
                    android:gravity="center" />
            </LinearLayout>


            <TextView
                android:layout_width="wrap_content" android:layout_height="wrap_content"
                android:layout_below="@id/tv_ref1"
                android:gravity="center"
                android:text="Text Text Text"
                android:textColor="@color/white" />
            <TextView
                android:layout_width="wrap_content" android:layout_height="wrap_content"
                android:layout_below="@id/tv_ref1"
                android:gravity="center_horizontal"
                android:text="Med"
                android:textColor="@color/white" />
            <TextView
                android:layout_width="wrap_content" android:layout_height="wrap_content"
                android:layout_below="@id/tv_ref1"
                android:gravity="center"
                android:text="Long Text wtwy yu hjsyi"
                android:textColor="@color/white" />

        </RelativeLayout>

Please let me know what I can do wrong, or how I can make my text images in a line have equal width.

Thank.

+5
source share
4 answers

layout_width "fill_parent" layout_weight "1" TextEdits

+22

max width . 20 30dip

0

First of all use:

xmlns:android="http://schemas.android.com/apk/res/android"

for the root layout, then check and change the code above when you repeat the textView after closing LinearLayout, which is not necessary, as I think.

0
source

get the maximum width among all text views and set this width for all text views

 @Override
        public void onWindowFocusChanged(boolean hasFocus) {
            // TODO Auto-generated method stub
            super.onWindowFocusChanged(hasFocus);
            // System.out.println("...111Height..."+mainLayout.getMeasuredHeight());

            width1 = textView1.getWidth();
    width2 = textView2.getWidth();
    width3 = textView3.getWidth();


    //get maximum width among all width
     int[] nums={width1 ,width2 ,width3};  
     Arrays.sort(nums);  
    int max =   nums[nums.length-1];

    //set this maximum value to all the textviews

    textView1.setWidth(max);
    textView2.setWidth(max);
    textView3.setWidth(max);

    }
0
source

All Articles