I'm designing my application UI. I need a layout looks like this:
我正在设计我的应用程序UI。我需要这样的布局:
(< and > are Buttons). The problem is, I don't know how to make sure the TextView will fill the remaining space, with two buttons have fixed size.
( <和> 是按钮)。问题是,我不知道如何确保TextView将填满剩余的空间,两个按钮的大小是固定的。
If I use fill_parent for Text View, the second button (>) can't be shown.
如果我对文本视图使用fill_parent,第二个按钮(>)将无法显示。
How can I craft a layout that looks like the image?
我如何设计一个看起来像图像的布局?
10 个解决方案
#1
142
Answer from woodshy worked for me, and it is simpler than the answer by Ungureanu Liviu since it does not use RelativeLayout
. I am giving my layout for clarity:
伍德希的回答对我很有效,而且它比Ungureanu Liviu的回答更简单,因为它不使用相对布局。我给出了我的布局清晰明了:
<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>
#2
86
In case if < TEXT VIEW > is placed in LinearLayout, set the Layout_weight proprty of < and > to 0 and 1 for TextView.
In case of RelativeLayout align < and > to left and right and set "Layout to left of" and "Layout to right of" property of TextView to ids of < and >
如果将< TEXT VIEW >放在LinearLayout中,则将 <和> 的Layout_weight proprty设置为0,TextView设置为1。如果是RelativeLayout align <和> 向左和向右对齐,则将TextView的属性设置为 <和> 的id“布局向左”和“布局向右”
#3
67
If you use RelativeLayout
, you can do it something like this:
如果你使用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>
#4
10
Using a ConstraintLayout
, I've found something like
使用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" />
works. The key is setting the right, left, top, and bottom edge constraints appropriately, then setting the width and height to 0dp
and letting it figure out it's own size.
的工作原理。关键是适当地设置右、左、顶和底边缘约束,然后将宽度和高度设置为0dp,并让它知道自己的大小。
#5
7
It´s simple You set the minWidth or minHeight, depends on what you are looking for, horizontal or vertical. And for the other object(the one that you want to fill the remaining space) you set a weight of 1 (set the width to wrap it´s content), So it will fill the rest of area.
它´s简单设置minWidth或minHeight,取决于你正在寻找什么,水平或垂直。和其他对象(一个你想填满剩余空间)设置的重量1(设置宽度来包装它´s内容),所以它将填补剩下的区域。
<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>
#6
4
you can use high layout_weight attribute. Below you can see a layout where ListView takes all free space with buttons at bottom:
您可以使用high 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>
#7
3
For those having the same glitch with <LinearLayout...>
as I did:
对于那些与 <线性布局> 有同样问题的人……>像我一样:
It is important to specify android:layout_width="fill_parent"
, it will not work with wrap_content
.
必须指定android:layout_width=“fill_parent”,它不能用于wrap_content。
OTOH, you may omit android:layout_weight = "0"
, it is not required.
OTOH,您可以省略android:layout_weight = "0",不需要。
My code is basically the same as the code in https://*.com/a/25781167/755804 (by Vivek Pandey)
我的代码基本上与https://*.com/a/25781167/755804中的代码相同(Vivek Pandey)
#8
0
You should avoid nesting 2 relative layout since relative layout always make 2 pass for drawing (against 1 for any other type of layout). It becomes exponential when you nest them. You should use linear layout with width=0 and weight=1 on the element you want to fill the space left. This answer is better for performance and the practices. Remember: use relative layout ONLY when you don't have other choice.
您应该避免嵌套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>
#9
0
You can use set the layout_width
or layout_width
to 0dp
(By the orientation you want to fill remaining space). Then use the layout_weight
to make it fill remaining space.
您可以将layout_width或layout_width设置为0dp(根据您希望填充剩余空间的方向)。然后使用layout_weight填充剩余的空间。
#10
0
use a Relativelayout to wrap 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>`
#1
142
Answer from woodshy worked for me, and it is simpler than the answer by Ungureanu Liviu since it does not use RelativeLayout
. I am giving my layout for clarity:
伍德希的回答对我很有效,而且它比Ungureanu Liviu的回答更简单,因为它不使用相对布局。我给出了我的布局清晰明了:
<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>
#2
86
In case if < TEXT VIEW > is placed in LinearLayout, set the Layout_weight proprty of < and > to 0 and 1 for TextView.
In case of RelativeLayout align < and > to left and right and set "Layout to left of" and "Layout to right of" property of TextView to ids of < and >
如果将< TEXT VIEW >放在LinearLayout中,则将 <和> 的Layout_weight proprty设置为0,TextView设置为1。如果是RelativeLayout align <和> 向左和向右对齐,则将TextView的属性设置为 <和> 的id“布局向左”和“布局向右”
#3
67
If you use RelativeLayout
, you can do it something like this:
如果你使用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>
#4
10
Using a ConstraintLayout
, I've found something like
使用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" />
works. The key is setting the right, left, top, and bottom edge constraints appropriately, then setting the width and height to 0dp
and letting it figure out it's own size.
的工作原理。关键是适当地设置右、左、顶和底边缘约束,然后将宽度和高度设置为0dp,并让它知道自己的大小。
#5
7
It´s simple You set the minWidth or minHeight, depends on what you are looking for, horizontal or vertical. And for the other object(the one that you want to fill the remaining space) you set a weight of 1 (set the width to wrap it´s content), So it will fill the rest of area.
它´s简单设置minWidth或minHeight,取决于你正在寻找什么,水平或垂直。和其他对象(一个你想填满剩余空间)设置的重量1(设置宽度来包装它´s内容),所以它将填补剩下的区域。
<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>
#6
4
you can use high layout_weight attribute. Below you can see a layout where ListView takes all free space with buttons at bottom:
您可以使用high 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>
#7
3
For those having the same glitch with <LinearLayout...>
as I did:
对于那些与 <线性布局> 有同样问题的人……>像我一样:
It is important to specify android:layout_width="fill_parent"
, it will not work with wrap_content
.
必须指定android:layout_width=“fill_parent”,它不能用于wrap_content。
OTOH, you may omit android:layout_weight = "0"
, it is not required.
OTOH,您可以省略android:layout_weight = "0",不需要。
My code is basically the same as the code in https://*.com/a/25781167/755804 (by Vivek Pandey)
我的代码基本上与https://*.com/a/25781167/755804中的代码相同(Vivek Pandey)
#8
0
You should avoid nesting 2 relative layout since relative layout always make 2 pass for drawing (against 1 for any other type of layout). It becomes exponential when you nest them. You should use linear layout with width=0 and weight=1 on the element you want to fill the space left. This answer is better for performance and the practices. Remember: use relative layout ONLY when you don't have other choice.
您应该避免嵌套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>
#9
0
You can use set the layout_width
or layout_width
to 0dp
(By the orientation you want to fill remaining space). Then use the layout_weight
to make it fill remaining space.
您可以将layout_width或layout_width设置为0dp(根据您希望填充剩余空间的方向)。然后使用layout_weight填充剩余的空间。
#10
0
use a Relativelayout to wrap 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>`