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="<"/>
<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>
Run Code Online (Sandbox Code Playgroud)
woo*_*shy 92
如果<TEXT VIEW>放置在LinearLayout中,则将<和>的Layout_weight属性设置为0,将TextView设置为1.
如果RelativeLayout将<和>左右对齐并将TextView的"Layout to left of"和"Layout to right of"属性设置为<和>的ID
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="<"
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>
Run Code Online (Sandbox Code Playgroud)
Tom*_*ard 28
使用a 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" />
Run Code Online (Sandbox Code Playgroud)
作品.关键是适当地设置右边,左边,顶边和底边约束,然后设置宽度和高度,0dp并让它找出它自己的大小.
很简单您可以设置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)
您可以使用高 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)
您应该避免嵌套 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="<" />
<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>
Run Code Online (Sandbox Code Playgroud)