Please don't mark this question as duplicate. Why? because I am going to explain why it's not.

Below are the steps I have done:

  1. Create a RelativeLayout
  2. Created LayoutPArams for it as wrap_content and wrap_content
  3. Created 1st TextView , gave it an id
  4. Created LayoutParams for first textview and added it to the RelativeLAyout
  5. Created Second TextView
  6. Created LayoutParams for it, added a rule to be right_Of of id_for_first_tetview
  7. Same step for Third Textview, except it's right_of the second textview

The problem here is: The first text view has text which extends to the second line, and when the text extends to the second line, the width of this i.e. the first textview occupied the whole width, and hence the other textview either overlaps or comes below it. Let me demonstrate that with a diagram:

Want:

This is what I want

Happening:

enter image description here

So in short, is there a way to keep the textviews in one row and even if one textview takes up two rows, the second textview can start right after where the text for forst Textview ended ?

Thank you in advance. I could really use some help.

有帮助吗?

解决方案

This can be done with LinearLayout with weight easily.

Method 1 - XML (give required padding or margins)

<LinearLayout
    android:layout_width="match_parent"
    android:weightSum="3"
    android:layout_height="wrap_content" >

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

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

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

</LinearLayout>

Method 2 - Programmatically (give required padding or margins)

tv2 = (TextView)findViewById(R.id.textView2);
tv3 = (TextView)findViewById(R.id.textView3);
tv4 = (TextView)findViewById(R.id.textView4);

android.widget.LinearLayout.LayoutParams params = new android.widget.LinearLayout.LayoutParams(
            LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 1f);

tv2.setLayoutParams(params);
tv3.setLayoutParams(params);
tv4.setLayoutParams(params);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top