线性布局:如何填充屏幕的整个宽度? [英] Linear Layout:How to fill the whole width of screen?
问题描述
我正在跟踪布局文件,该文件在相对布局内创建线性布局.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:background="#eeeeee">
<LinearLayout // I want this to fill the whole width of screen.
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="50px"
android:background="#666666"
>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/newEvent"
android:hint="Enter the Name of Event"
android:layout_below="@+id/textView2"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select Location"
android:id="@+id/pickerButton"
android:layout_alignTop="@+id/newEvent"
android:layout_toEndOf="@+id/newEvent" />
</LinearLayout>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Set Remainder"
android:id="@+id/submit"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="View All"
android:id="@+id/view"
android:layout_below="@+id/submit"
android:layout_centerHorizontal="true"
android:layout_marginTop="68dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Your Location Will Appear Here"
android:id="@+id/textView4"
android:layout_above="@+id/submit"
android:layout_centerHorizontal="true"
android:layout_marginBottom="48dp" />
</RelativeLayout>
使用此代码,我得到了以下布局:
但是在灰色(线性布局)的左侧,右侧和顶部左侧留有空格.我不要这些空间.我想用灰色填充整个宽度.我应该在xml
中进行哪些更改以实现此目标?
提前谢谢!
感谢您的回答.但是解决此问题之后,我将文本框和按钮放在灰色框的底部,如下图所示:
为此,我已经尝试了"android:gravity="center_horizontal"
,"android:gravity="center_vertical"
和"android:gravity="center"
.我也尝试过padding-bottom
.
但这不起作用.
删除
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
来自父级RelativeLayout
行
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="50px"
android:background="#666666"
android:orientation="horizontal"
android:gravity="center_vertical"
>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/newEvent"
android:hint="Enter the Name of Event"
android:layout_gravity="center_vertical" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select Location"
android:id="@+id/pickerButton"
android:layout_gravity="center_vertical" />
</LinearLayout>
I've following layout file, which creates linear layout inside the relative layout.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:background="#eeeeee">
<LinearLayout // I want this to fill the whole width of screen.
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="50px"
android:background="#666666"
>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/newEvent"
android:hint="Enter the Name of Event"
android:layout_below="@+id/textView2"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select Location"
android:id="@+id/pickerButton"
android:layout_alignTop="@+id/newEvent"
android:layout_toEndOf="@+id/newEvent" />
</LinearLayout>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Set Remainder"
android:id="@+id/submit"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="View All"
android:id="@+id/view"
android:layout_below="@+id/submit"
android:layout_centerHorizontal="true"
android:layout_marginTop="68dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Your Location Will Appear Here"
android:id="@+id/textView4"
android:layout_above="@+id/submit"
android:layout_centerHorizontal="true"
android:layout_marginBottom="48dp" />
</RelativeLayout>
Using this code, I got following layout:
But There are spaces left in left, right and top of the gray color (linear-Layout). I don't want these space. I want to fill the whole width with gray color. What changes should I make in my xml
to achieve this?
Thanks in advance!
EDIT:
Thanks for the answers. But after resolving this, I'm getting my text box and button at the bottom of the gray box, as shown in following figure:
For this, I've already tried "android:gravity="center_horizontal"
, "android:gravity="center_vertical"
and "android:gravity="center"
. I've also tried padding-bottom
.
But this is not working.
Remove
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
lines from parent RelativeLayout
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="50px"
android:background="#666666"
android:orientation="horizontal"
android:gravity="center_vertical"
>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/newEvent"
android:hint="Enter the Name of Event"
android:layout_gravity="center_vertical" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select Location"
android:id="@+id/pickerButton"
android:layout_gravity="center_vertical" />
</LinearLayout>
这篇关于线性布局:如何填充屏幕的整个宽度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!