![](“https://istack.dev59.com/t5Ulu.webp”alt)
如果我为Text View使用fill_parent,则第二个按钮(>)无法显示。
如何创建一个类似于图片的布局?
来自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="<"/>
<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=">"/>
</LinearLayout>
alignLeft
和alignParentLeft
等的东西,是无法通过LinearLayout
实现的。 - IcyFlame< TextView >
放置在 LinearLayout
中,则应将<
和>
的 Layout_weight
属性设置为0和1,以便于 TextView
的显示。 RelativeLayout
,请将<
和>
向左和向右对齐,并将TextView
的“Layout to left of”和“Layout to right of”属性设置为<
和>
的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="<"
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=">"
android:layout_alignParentRight = "true"/>
</RelativeLayout>
</RelativeLayout>
>
按钮? - Luke Vo使用 ConstraintLayout
,我发现类似以下的问题:
<Button
android:id="@+id/left_button"
android:layout_width="80dp"
android:layout_height="48dp"
android:text="<"
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=">"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
有效的关键是适当地设置右、左、上和下边缘约束,然后将宽度和高度设置为0dp
并使其自行确定大小。
TextView
具有左右约束。如果第一个Button
与TextView
具有右约束,最后一个Button
与TextView
具有左约束,则TextView
不会被拉伸。 - Manuel这很简单,您可以根据需要设置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>
您可以使用高的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>
应避免嵌套两个相对布局,因为相对布局在绘制时始终需要进行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="<" />
<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=">" />
</LinearLayout>
</LinearLayout>
如果您遇到与我相同的<LinearLayout...>
问题:
重要的是要指定android:layout_width="fill_parent"
,而不是wrap_content
。
另一方面,您可以省略android:layout_weight="0"
,它并非必需的。
我的代码基本上与https://dev59.com/SW015IYBdhLWcg3w6QLA#25781167中的Vivek Pandey的代码相同。
<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"/>
您可以使用设置layout_width
或layout_height
为0dp
(根据您想要填充剩余空间的方向)。
然后使用layout_weight
来使其填充剩余空间。