Android 如何使视图布局填充剩余空间?

Android 如何使视图布局填充剩余空间?,android,layout,android-layout,Android,Layout,Android Layout,我正在设计我的应用程序界面。我需要这样的布局: <RelativeLayout android:layout_width = "fill_parent" android:layout_height = "fill_parent"> <ImageView android:id = "@+id/my_image" android:layout_width = "wrap_content" android:lay

我正在设计我的应用程序界面。我需要这样的布局:

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

(<和>是按钮)。问题是,我不知道如何确保TextView会填满剩余的空间,因为两个按钮的大小是固定的

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


如何创建一个与图像相似的布局?

如果被放置在LinearLayout中,请将TextView的布局权重属性设置为0和1。
如果是RelativeLayout,请将向左和向右对齐,并将“layout to left of”和“layout to right of”设置为“layout to left of”属性将TextView的ID设置为<和>

如果使用
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>

这很简单 您可以设置最小宽度或最小高度,具体取决于您要查找的对象,水平或垂直。 对于另一个对象(您希望填充剩余空间的对象),您将权重设置为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>


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>

对于那些与我有相同问题的人:

重要的是要指定
android:layout\u width=“fill\u parent”
,它将不适用于
wrap\u内容

哦,您可以省略android:layout\u weight=“0”
,这不是必需的


我的代码基本上与(Vivek Pandey)中的代码相同。

您可以使用“高布局权重”属性。下面您可以看到一个布局,其中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>

您应该避免嵌套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>

您可以使用将
布局宽度
布局宽度
设置为
0dp
(按要填充剩余空间的方向)。
然后使用
布局\u权重
填充剩余空间。

使用相对线布局来包裹线性布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:round="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical">
    <Button
        android:layout_width = "wrap_content"
        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 = "wrap_content"
        android:layout_height = "wrap_content"
        android:text="&gt;"/>

</LinearLayout>

</RelativeLayout>`

`

使用
ConstraintLayout
,我发现了如下内容


工作。关键是适当设置右、左、上、下边缘约束,然后将宽度和高度设置为
0dp
,让它计算出自己的大小。

 <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginEnd="10dp"
        android:fontFamily="casual"
        android:text="(By Zeus B0t)"
     ``   android:textSize="10sp"
        android:gravity="bottom"
        android:textStyle="italic" />

使用相对布局时,可以通过将视图锚定到它应该拉伸的两个视图来拉伸视图。虽然指定的高度将被忽略,但Android仍然需要一个height属性,这就是我写“0dp”的原因。例如:

<View
    android:id="@+id/topView"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:layout_alignParentTop="true"
    android:layout_marginTop="8dp"/>

<View
    android:id="@+id/stretchableView"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_below="@id/topView"
    android:layout_above="@+id/bottomView"
    android:layout_marginTop="8dp"
    android:layout_marginBottom="8dp"
    android:adjustViewBounds="true"/>

<View
    android:id="@id/bottomView"
    android:layout_width="wrap_content"
    android:layout_height="40dp"
    android:layout_alignParentBottom="true"
    android:layout_marginBottom="16dp"/>



你用什么作为根布局?@woodshy任何布局都可以,没关系。谢谢,它可以:)但我不明白。为什么文本视图没有填满所有的空间,而不是
按钮?为什么有两个嵌套的相对布局?根1应该足够了。然后,您需要做的就是确保底部相对布局的子级都是alignParentBottom=“true”@Noel您是对的。我选择2布局,因为这是我使用的方式:在我的应用程序中,可能需要更改一些视图的可见性,并且更容易将所有视图放置在一个布局中,并仅更改此布局的可见性。我的示例并不完美,但它可以工作,并且可能对某些人有用。@W.N这对我来说也是一种说法:)我认为textview没有使用所有的空间,因为右边的按钮有固定的宽度。如果你把右键的android:layout\u width=“80dp”改为android:layout\u width=“wrap\u content”,那就行了。这个答案太糟糕了!您应该避免嵌套2个相对布局,因为相对布局总是使图形通过2次(而任何其他类型的布局通过1次)。当你把它们嵌套起来时,它就成了指数。对于要填充剩余空间的图元,应使用宽度为0、重量为1的线性布局。记住:只有在没有其他选择时才使用相对布局。还有人需要样本吗?如何在这个布局中添加图像以填充整个视图如何在相对布局中添加图像以填充整个视图view@woodshy是的,一个样本仍然是有用的看看Vivek Pandey的答案,因为有些东西像
alignLeft
alignParentLeft
,等等,使用
线性布局
永远无法实现这一点。我不完全理解这是怎么可能的,但这确实是避免相对性的很酷的技巧。这怎么可能是公认的答案?没有图像视图,它只是解决方案的一部分。至少它比以前的相对布局排名靠前的答案(我在回答中解释了原因)要好genius Solution刚开始使用ConstraintLayout,很高兴知道宽度和高度可以由约束决定,当它们设置为“0”时,Thanx:重要的是要注意每个约束的箭头方向。确保
TextView
具有左右约束。如果第一个
按钮对
文本视图
具有右约束,最后一个
按钮对
文本视图
具有左约束,则
文本视图
不会拉伸。更好!通过不使用嵌套布局来提高性能。感谢您提醒我们0dp技巧