我正在设计我的应用程序UI。我需要一个这样的布局:
(<和>是按钮)。问题是,我不知道如何确保TextView将填补剩余的空间,与两个按钮有固定的大小。
如果我使用fill_parent的文本视图,第二个按钮(>)不能显示。
我如何制作一个看起来像图像的布局?
我正在设计我的应用程序UI。我需要一个这样的布局:
(<和>是按钮)。问题是,我不知道如何确保TextView将填补剩余的空间,与两个按钮有固定的大小。
如果我使用fill_parent的文本视图,第二个按钮(>)不能显示。
我如何制作一个看起来像图像的布局?
如果<TextView>被放置在线性布局中,设置<和>的Layout_weight属性为0和1的TextView。
如果你使用RelativeLayout,对齐<和>到左边和右边,并设置“布局到左边”和“布局到右边”属性的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="<"
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>
它´s简单 你设置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>
来自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>
对于那些有相同的故障与<线性布局…>就像我做的那样:
重要的是指定android:layout_width="fill_parent",它将不能与wrap_content工作。
哦,你可以省略android:layout_weight = "0",这不是必需的。
我的代码基本上与https://stackoverflow.com/a/25781167/755804中的代码相同(by Vivek Pandey)
你可以使用高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相对布局,因为相对布局总是使2通过绘图(相对于1任何其他类型的布局)。当你嵌套它们时,它就变成了指数。你应该在元素上使用width=0和weight=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>
您可以使用将layout_width或layout_width设置为0dp(通过您想要填充剩余空间的方向)。 然后使用layout_weight使其填充剩余空间。
使用Relativelayout来包装LinearLayout
<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="<"/>
<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=">"/>
</LinearLayout>
</RelativeLayout>`
使用一个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
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仍然需要一个高度属性,这就是为什么我写“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"/>