【问题标题】:Layout design problem版面设计问题
【发布时间】:2011-08-25 09:48:28
【问题描述】:

我正在设计布局,但遇到了问题。不知何故,它在我的表格布局右侧有一个空白空间,我无法填充它。如果有人有想法,请您帮帮我吗?

下面是我的布局 xml 和屏幕截图,以显示究竟发生了什么;顺便说一句,我使用 tablelayout 的原因是为了通过与用户的交互将一些项目放在那里。

提前谢谢你


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

<LinearLayout android:id="@+id/header" android:layout_height="wrap_content" android:layout_width="match_parent">
    <TextView android:id="@+id/header_life_title" android:textSize="20sp" android:text="Life: " android:layout_height="wrap_content" android:layout_width="wrap_content"></TextView>
    <TextView android:id="@+id/header_life_value" android:textSize="20sp" android:text="20" android:layout_height="wrap_content" android:layout_width="wrap_content"></TextView>
    <Button android:text="Inc." android:id="@+id/header_btn_inc" android:layout_width="wrap_content" android:layout_height="35dp" android:layout_gravity="center_vertical">
    <Button android:text="Dec." android:id="@+id/header_btn_dec" android:layout_width="wrap_content" android:layout_height="35dp"></Button>
    <Button android:text="Put L." android:id="@+id/header_btn_putLand" android:layout_width="wrap_content" android:layout_height="35dp"></Button>
    <Button android:text="Remove L." android:id="@+id/header_btn_removeLand" android:layout_width="wrap_content" android:layout_height="35sp">
    <LinearLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:id="@+id/land_space">
    </LinearLayout>
</LinearLayout>
    <ScrollView android:id="@+id/scrollView1" android:layout_height="wrap_content" android:layout_width="match_parent">
        <TableLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/cardboard">
            <LinearLayout android:layout_height="40dp" android:layout_width="match_parent" android:id="@+id/layout_menu">
                <Spinner android:id="@+id/spinner_creature" android:layout_height="wrap_content" android:layout_width="150dp"></Spinner>
                <Button android:id="@+id/btn_creature" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="Cast"></Button>
                <Spinner android:id="@+id/spinner_noncreature" android:layout_height="wrap_content" android:layout_width="wrap_content"></Spinner>
                <Button android:id="@+id/btn_noncreature" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="Cast"></Button>
                <Button android:id="@+id/btn_delete" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="Del."></Button>
            </LinearLayout>
            <TableRow android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/tableRow_cardImage"><![enter image description here][1]/TableRow>
            <TableRow android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/tableRow_cardData"></TableRow>
            <TableRow android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/tableRow_cardImage2"></TableRow>
            <TableRow android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/tableRow_cardData2"></TableRow>
        </TableLayout>
    </ScrollView>
</LinearLayout>

@987654321@

【问题讨论】:

  • 那个空间不是由LinearLayout而不是TableLayout造成的吗?
  • 感谢您的评论,卡斯帕。如果可能,您能告诉我如何解决这个问题吗?
  • 你试试 layout_width="fill_parent" 最后一个元素:Del.? (只是猜测)
  • 第二行的按钮/微调器是否应该填充视图?
  • 我认为只是其中一个,我会选择最小的一个,以防止在小屏幕或纵向模式下剪切文本。

标签: android layout


【解决方案1】:

您可以使用LinearLayoutlayout_weight 属性来强制其中的视图占用剩余空间。

将所有子视图的 layout_weight 属性设置为 "1" 将使它们平均占用剩余空间:

<LinearLayout android:layout_height="40dp"
    android:layout_width="match_parent" android:id="@+id/layout_menu">
    <Spinner android:id="@+id/spinner_creature"
        android:layout_height="wrap_content" android:layout_weight="1"
        android:layout_width="150dp"></Spinner>
    <Button android:id="@+id/btn_creature"
        android:layout_height="wrap_content" android:layout_weight="1"
        android:layout_width="wrap_content" android:text="Cast"></Button>
    <Spinner android:id="@+id/spinner_noncreature"
        android:layout_height="wrap_content" android:layout_weight="1"
        android:layout_width="wrap_content"></Spinner>
    <Button android:id="@+id/btn_noncreature"
        android:layout_height="wrap_content" android:layout_weight="1"
        android:layout_width="wrap_content" android:text="Cast"></Button>
    <Button android:id="@+id/btn_delete"
        android:layout_height="wrap_content" android:layout_weight="1"
        android:layout_width="wrap_content" android:text="Del."></Button>
</LinearLayout>

子视图的宽度设置为wrap_content,所以当你改变手机的方向时它们不会变宽,即使父视图有更多的水平空间。
使用layout_weight,您可以为每个子视图设置父视图的剩余空间应填充多少。

更多参考:LinearLayout.LayoutParams

【讨论】:

  • 感谢您的帮助,rekaszeru!现在我解决了这个问题。也感谢您的帮助,Caspar!
  • 哇,我刚刚检查了你修改过的这条评论。也感谢您的参考!
猜你喜欢
  • 1970-01-01
  • 2011-09-28
  • 1970-01-01
  • 2013-12-08
  • 2010-12-27
  • 1970-01-01
相关资源
最近更新 更多