开发者

Android horizontal LinearLayout with wrapped text in TextView

I've observed a behavior with layout_weight that I can't explain. The following is a trivial example:

<?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="wrap_content"
    android:orientation="horizontal"
>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:text="This is a very very very very very very very very very very very very very very very long string." 
        android:layout_weight="1"
    />

    <View
        android:layout_width="32dp"
        android:layout_height="32dp"
        android:layout_gravity="center_vertical"
        android:background="#ffffffff"
    />

</LinearLayout>

In a QVGA display, the TextView wraps the text. The white square is displayed to the right of the text.

However, if I remove android:layout_weight="1" from the TextView, the TextView now takes up the entire display width. The white square is no longer displayed.

  1. Why would layout_weight in the TextView affect whether or not the white square is displayed? Shouldn't the View with the white b开发者_如何学Goackground always be assigned 32dpx32dp first? (It makes no difference if the view were any other types - ImageView or TextView).
  2. The problem I was working on is that I want the white square to always be displayed to the right of the TextView (whether or not the text is wrapped), but I don't want any empty space between the TextView and the white square. (If I add android:layout_weight="1" to the TextView, then there is a gap if the text is not wrapped.)

Any help would be appreciated!


To answer my question #1: One thing I learned by looking at the source for LinearLayout: Not only does layout_weight assign unused space to a child, it also shrinks a child with layout_weight if the child extends beyond the bounds of the LinearLayout. That explains why a TextView with wrapped text is shrunk in my layout.

As for the answer to my question #2, I think you meant android:toRigthOf instead of android:layout_alignRight. Using a RelativeLayout instead of a LinearLayout doesn't change the layout behavior. The tricky part is placing a view immediately to the right of a TextView, without gaps, whether or not the text is wrapped. Setting a maxWidth would limit the TextView's width, but that solution doesn't scale across portrait/landscape and different display dimensions.

Solution - Looks like Dyarish's solution is the best available. My layout problem exists regardless of the layout you use. The key is to set a maxWidth for the TextView so that it doesn't take up the all of the horizontal space in the layout. Because hardcoding a android:maxWidth value in the TextView doesn't scale across different displays, setting the maxWidth at runtime, as Dyarish suggested, is a good solution.


Hopefully this is what you are looking for.

First off, here is a great resource I found for Creating UI's.

  1. layout_weight - Specifies how much of the extra space in the layout to be allocated to the View.

  2. If you want to ensure that the white square is always to the right of the textview, you can use a Relative View, and add the parameter to the view. android:layout_alignRight="+id@yourTextViewID". This should always make the box appear right beside the textView area. You should probably also add something like android:maxWidth="250px" This will ensure that you don't push the white box completely out of the screen.

Here is a code sample:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
>

    <TextView
        android:layout_width="wrap_content"
        android:maxWidth="250px"
        android:id="@+id/TextForWhiteBox"
        android:layout_height="wrap_content"
        android:layout_gravity="center|left"
        android:text="This is a very very very very very very very very very very very very very very very long string."   
    />

    <View android:background="#ffffffff" android:layout_width="32dp" android:layout_height="32dp" android:id="@+id/view1" android:layout_toRightOf="@+id/TextForWhiteBox"></View>
    </RelativeLayout>

You could also add to the View:

android:layout_alignTop="@+id/TextForWhiteBox" android:layout_alignBottom="@+id/TextForWhiteBox"

to make the white box the same size as the TextView.


  1. Firstly I've tested the code from my other answer and it does exactly what you've described you've wanted. (unless I'm misunderstanding what you are asking for). You definitely do not want to use the android:layout_alignRight which is not what is in the code sample. That would simply keep the box on the right hand of the screen and not be affected by the textview at all. This sample uses android:layout_toRightOf="@+id/TextForWhiteBox" which is possible due to it being a relative layout. Since the Relative Layout allows you to place objects in relation to others. That line will always place the box just to the right of the textview with no gaps.

  2. As for the screen orientation changes:

When the orientation changes it creates a new instance of the view.

Here is a simple solution.

//Add to oncreate in your Activity

     private TextView textStatus;   
     textStatus = (TextView) findViewById(R.id.TextForWhiteBox); 


// This get's the width of your display.
DisplayMetrics displaymetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
        int width = displaymetrics.widthPixels;

// Now you know the screen orientation, and it's width. So just set the maxwidth of the text view to match the display width - the pixels of your white box. 

     textStatus.setMaxWidth(width - 32); // 32 is here because you already know the size of the white box. More logic is needed to dynamically get this value, because you would need to wait for the activity to be fully created.
}

Here is the main.xml I used:

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

>
    <TextView
        android:layout_width="wrap_content"

        android:id="@+id/TextForWhiteBox"
        android:layout_height="wrap_content"
        android:layout_gravity="center|left"
        android:text="This is a very very very very very very very very very very very very very very very very very very very long string."   
    />

    <View android:background="#ffffffff" android:layout_width="32px" android:layout_height="32px" android:id="@+id/view1" android:layout_toRightOf="@+id/TextForWhiteBox"></View>
    </RelativeLayout>

You might need some additional logic to keep screen values.

This code has been tested, you should be able to literally copy and paste this to work as you asked.

Also depending on your logic you could use something like this to return the screen orientation.

int orient = getResources().getConfiguration().orientation;

Hope this helps!

If this helped you, please click the accepted button. =) Cheers!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜