Android Layout_Weight for Fixed Width Not Working

懵懂的女人 提交于 2020-01-16 19:14:25

问题


I'm trying to use layout_weight and layout_width with a linear layout with horizontal orientation. The linear layout contains 1 TextView and 2 RadioButtons (in a RadioButtonGroup). I'd like the TextView to fill up the space to the left of the RadioButtons and always keep the RadioButtons right-justified on the screen. So regardless of the amount of text, the TextView should take up the space so that the RadioButtons are always right-justified. The LinearLayout is used in a ListView to have multiple rows in the list.

Unfortunately, the TextView is wrapping content despite specifying android:layout_width="0dp" and android:weight="1". And the RadioButtons are not lined up on the right nicely.
Here's the image

Here's the XML for the LinearLayout.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:orientation="horizontal"
   android:scrollbarAlwaysDrawVerticalTrack="false" >

    <TextView
        android:id="@+id/jokeTextView"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="match_parent"
        android:gravity="center_vertical"/>

    <RadioGroup
        android:id="@+id/ratingRadioGroup"
        android:layout_width="35dp"
        android:layout_height="wrap_content"
        android:gravity="right"
        android:orientation="vertical" >

        <RadioButton
            android:id="@+id/likeButton"
            android:layout_width="35dp"
            android:layout_height="wrap_content"
            android:button="@drawable/like" 
            android:gravity="right"/>

        <RadioButton
            android:id="@+id/dislikeButton"
            android:layout_width="35dp"
            android:layout_height="wrap_content"
            android:button="@drawable/dislike" 
            android:gravity="right"/>

    </RadioGroup>

</LinearLayout>

I also tried it with setting RadioButton android:layout_width="wrap_content" which did not work either. Any help would be appreciated.


回答1:


Try this..

I guess your listview width as wrap_content change it as match_parent and try

<ListView 
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />



回答2:


Issue is with your listview. There is no issue with the item layout

Change your listview

 android:layout_width="match_parent""



回答3:


If you want a layout on one side and one on the other, use RelativeLayout: alignParentRight/Left.



来源:https://stackoverflow.com/questions/22725258/android-layout-weight-for-fixed-width-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!