问题描述
|
我正在设计我的应用程序UI。我需要这样的布局:
(<和>是按钮)。问题是,我不知道如何确保TextView会填充剩余的空间,两个按钮的大小固定。
如果我在文本视图中使用fill_parent,则不会显示第二个按钮(>)。
如何制作看起来像图像的布局?
解决方法
woodshy的答案对我有用,它比Ungureanu Liviu的答案更简单,因为它不使用放置在LinearLayout中,则将TextView的Layout_weight属性设置为<和>,将0和1设置为1。如果使用RelativeLayout,则将<和>左右对齐,并将\“ Layout设置为\” TextView的\“ Layout to right of \”属性为<和>的ID
, 如果使用
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>
, 如果将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>
, 使用a4ѭ,我发现类似
<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
,然后计算出自己的大小。
, 这很简单
设置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>
, 对于那些像我一样对ѭ9产生相同干扰的人:
指定ѭ10specify很重要,wrap_content
不起作用。
OTOH,您可以省略android:layout_weight = \"0\"
,这不是必需的。
我的代码与https://stackoverflow.com/a/25781167/755804中的代码基本相同(作者:Vivek Pandey)
, 您应该避免嵌套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
(根据要填充剩余空间的方向)。
然后使用ѭ17填充剩余空间。
, 使用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>`
, 我发现
<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\" />