固定宽度的Android Layout_Weight不起作用 [英] Android Layout_Weight for Fixed Width Not Working

查看:96
本文介绍了固定宽度的Android Layout_Weight不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将layout_weight和layout_width与水平方向的线性布局一起使用.线性布局包含1个TextView和2个RadioButton(在RadioButtonGroup中).我希望TextView能够填充RadioButtons左侧的空间,并始终使RadioButtons在屏幕上保持右对齐.因此,无论文本多少,TextView都应占用空间,以使RadioButtons始终右对齐.在ListView中使用LinearLayout在列表中具有多行.

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.

不幸的是,尽管指定了android:layout_width ="0dp"和android:weight ="1",但TextView仍在包装内容.而且RadioButtons不在右侧排列好.
这是图片

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

这是LinearLayout的XML.

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>

我也尝试通过设置RadioButton android:layout_width ="wrap_content"来尝试它,该方法也不起作用.任何帮助将不胜感激.

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

推荐答案

尝试一下..

我猜您的 listview 宽度为wrap_content,将其更改为match_parent并尝试

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" />

这篇关于固定宽度的Android Layout_Weight不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆