如何使用View填充剩余空间?

Luk*_* Vo 182 layout android android-layout

我正在设计我的应用程序UI.我需要一个如下布局:

所需布局的示例

(<和>是按钮).问题是,我不知道如何确保TextView将填充剩余空间,两个按钮具有固定大小.

如果我在文本视图中使用fill_parent,则无法显示第二个按钮(>).

如何制作看起来像图像的布局?

Viv*_*dey 198

来自woodshy的回答为我工作,它比Ungureanu Liviu的回答简单,因为它不使用RelativeLayout.为了清晰起见,我给出了我的布局:

<LinearLayout 
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:orientation="horizontal"
      >

     <Button
        android:layout_width = "80dp"
        android:layout_weight = "0"
        android:layout_height = "wrap_content"
        android:text="&lt;"/>
     <TextView
        android:layout_width = "fill_parent"
        android:layout_height = "wrap_content"
        android:layout_weight = "1"/>
     <Button
        android:layout_width = "80dp"
        android:layout_weight = "0"
        android:layout_height = "wrap_content"
        android:text="&gt;"/>   
 </LinearLayout>
Run Code Online (Sandbox Code Playgroud)

  • 这是一个男人在线性布局中理解体重的真实工作的那一天.:P(y) (9认同)
  • 这怎么可以接受?没有图像视图,它只是解决方案的一部分.至少它比相对布局的旧顶级投票答案更好(我在答案中解释了原因) (2认同)

woo*_*shy 92

如果<TEXT VIEW>放置在LinearLayout中,则将<和>的Layout_weight属性设置为0,将TextView设置为1.
如果RelativeLayout将<和>左右对齐并将TextView的"Layout to left of"和"Layout to right of"属性设置为<和>的ID

  • @woodshy是的样本仍然会有所帮助 (4认同)
  • 这是正确的答案,但一个例子会很好:) (3认同)
  • 还有人需要样品吗? (2认同)

Ung*_*viu 71

如果你使用RelativeLayout,你可以这样做:

<RelativeLayout
    android:layout_width = "fill_parent"
    android:layout_height = "fill_parent">
    <ImageView
        android:id = "@+id/my_image"
        android:layout_width = "wrap_content"
        android:layout_height = "wrap_content"
        android:layout_alignParentTop ="true" />
    <RelativeLayout
        android:id="@+id/layout_bottom"
        android:layout_width="fill_parent"
        android:layout_height = "50dp"
        android:layout_alignParentBottom = "true">
        <Button
            android:id = "@+id/but_left"
            android:layout_width = "80dp"
            android:layout_height = "wrap_content"
            android:text="&lt;"
            android:layout_alignParentLeft = "true"/>
        <TextView
            android:layout_width = "fill_parent"
            android:layout_height = "wrap_content"
            android:layout_toLeftOf = "@+id/but_right"
            android:layout_toRightOf = "@id/but_left" />
        <Button
            android:id = "@id/but_right"
            android:layout_width = "80dp"
            android:layout_height = "wrap_content"
            android:text="&gt;"
            android:layout_alignParentRight = "true"/>
    </RelativeLayout>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

  • 为什么有两个嵌套的相对布局?根本就足够了.然后,您需要做的就是确保底部相对布局的子项都是alignParentBottom ="true" (2认同)
  • 这个答案非常糟糕!您应该避免嵌套2相对布局,因为相对布局总是为绘图传递2次(对于任何其他类型的布局,则为1).当你嵌套它时它会变成指数.您应该在要填充剩余空间的元素上使用width = 0和weight = 1的线性布局.请记住:仅在您没有其他选择时才使用相对布局.http://stackoverflow.com/questions/4069037/android-is-a-relativelayout-more-expensive-than-a-linearlayout (2认同)

Tom*_*ard 28

使用a ConstraintLayout,我发现了类似的东西

<Button
    android:id="@+id/left_button"
    android:layout_width="80dp"
    android:layout_height="48dp"
    android:text="&lt;"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toRightOf="@+id/left_button"
    app:layout_constraintRight_toLeftOf="@+id/right_button"
    app:layout_constraintTop_toTopOf="parent" />

<Button
    android:id="@+id/right_button"
    android:layout_width="80dp"
    android:layout_height="48dp"
    android:text="&gt;"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
Run Code Online (Sandbox Code Playgroud)

作品.关键是适当地设置右边,左边,顶边和底边约束,然后设置宽度和高度,0dp并让它找出它自己的大小.

  • 刚开始使用ConstraintLayout,很高兴知道宽度和高度可以通过约束来设置为"0",Thanx为那:) (4认同)

Dan*_*iel 7

很简单您可以设置minWidth或minHeight,取决于您要查找的内容,水平或垂直.对于另一个对象(要填充剩余空间的对象),将权重设置为1(设置宽度以包装其内容),因此它将填充其余区域.

<LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center|left"
        android:orientation="vertical" >
</LinearLayout>
<LinearLayout
        android:layout_width="80dp"
        android:layout_height="fill_parent"
        android:minWidth="80dp" >
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)


thi*_*ker 5

您可以使用高 layout_weight 属性。您可以在下面看到一个布局,其中 ListView 使用底部的按钮占用所有可用空间:

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

        <ListView
            android:id="@+id/listView"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1000"
            />


        <Button
            android:id="@+id/btnCreateNewRule"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Create New Rule" />



        <Button
            android:id="@+id/btnConfigureOk"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Ok" />


</LinearLayout>
Run Code Online (Sandbox Code Playgroud)


Liv*_*vio 5

您应该避免嵌套 2 个相对布局,因为相对布局总是为绘图进行 2 遍(相对于任何其他类型的布局为 1 遍)。当你嵌套它们时,它会呈指数增长。您应该在要填充剩余空间的元素上使用宽度=0 和重量=1 的线性布局。这个答案对于性能和实践来说更好。请记住:仅当您没有其他选择时才使用相对布局。

<?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="match_parent"
          android:orientation="vertical">

    <ImageView
        android:id="@+id/imageview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="horizontal">

        <Button
            android:id="@+id/prev_button"
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:text="&lt;" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ellipsize="end"
            android:singleLine="true"
            android:gravity="center"
            android:text="TextView" />

        <Button
            android:id="@+id/next_button"
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:text="&gt;" />
    </LinearLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)