Android 布局问题 - 使用权重的相对宽度百分比
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2914741/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android layout issue - relative widths in percent using weight
提问by cdonner
I am trying to assign relative widths to columns in a ListView that is in a TabHost, using layout_weight as suggested here:
我正在尝试使用此处建议的 layout_weight 为 TabHost 中的 ListView 中的列分配相对宽度:
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TabWidget android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<FrameLayout android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableLayout
android:id="@+id/triplist"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="4px">
<TableRow>
<ListView android:id="@+id/triplistview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</TableRow>
<TableRow>
<Button android:id="@+id/newtripbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Trip"/>
</TableRow>
[other tabs ...]
My row definition has 4 columns that I would like to size as follows:
我的行定义有 4 列,我想按如下方式调整大小:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1.0"
android:padding="4px">
<TextView android:id="@+id/rowtripdate"
android:layout_weight=".2"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:inputType="date"/>
<TextView android:id="@+id/rowodostart"
android:layout_weight=".2"
android:layout_width="0dip"
android:layout_height="wrap_content"/>
<TextView android:id="@+id/rowodoend"
android:layout_weight=".2"
android:layout_width="0dip"
android:layout_height="wrap_content"/>
<TextView android:id="@+id/rowcomment"
android:layout_weight=".4"
android:layout_width="0dip"
android:layout_height="wrap_content">
Unfortunately, it seems to want to fit all the columns into the space that the button occupies, as opposed to the width of the screen. Or maybe there is another constraint that I do not understand. I'd appreciate your help.
不幸的是,它似乎想要将所有列放入按钮占用的空间中,而不是屏幕的宽度。或者也许还有另一个我不明白的限制。我很感激你的帮助。
(source: heeroz.com)
(来源:heeroz.com)
采纳答案by cdonner
I think I forgot this property for the TableLayout element:
我想我忘记了 TableLayout 元素的这个属性:
android:stretchColumns="0"
It appears to be working now.
它现在似乎正在工作。
回答by Cheryl Simon
I don't see anything obviously wrong in what you are showing there. To get a better picture of what is going on, you might try out the heirarchy viewer. It comes with the SDK. It will visualize for you how much space each of the "hidden" layers is taking, so you can figure out which one decided to only be that big.
我看不出你在那里展示的东西有什么明显的错误。为了更好地了解正在发生的事情,您可以尝试使用层次结构查看器。它随 SDK 一起提供。它将为您可视化每个“隐藏”层占用了多少空间,因此您可以确定哪个决定只有那么大。