android - 线性布局 :How to fill the whole width of screen?

标签 android xml android-layout android-linearlayout

我有以下布局文件,它在相关布局内创建线性布局。

<?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>

使用这段代码,我得到了以下布局:

enter image description here

但是在灰色的左边、右边和顶部留有空格(线性布局)。我不想要这些空间。我想用灰色填充整个宽度。我应该在我的 xml 中进行哪些更改才能实现此目的?

提前致谢!

编辑:

感谢您的回答。但是在解决这个问题之后,我在灰色框的底部得到了我的文本框和按钮,如下图所示:

enter image description here

为此,我已经尝试过 "android:gravity="center_horizo​​ntal""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>

关于android - 线性布局 :How to fill the whole width of screen?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38900772/

相关文章:

android - 在我的测试用例中启动服务时未调用 onStartCommand()

android - 如何使用 flutter 访问 JSON 对象?

python - 从 SOAP 响应中解析多个值

xml - 如何使用 XPath 2.0 识别数字序列中的重复值?

android - 将 LinearLayout 对齐到 DrawerLayout 的底部

java - Android SQLite : Running the Same Query with Different Parameters Multiple Times Efficiently

java - 下载后打开文件APK

java - Android:具有现有布局的自定义 View 的多个 View 子项

Android:减少 GridView 中列之间的空间

java - 我在 android studio 中使用什么来进行多个 Activity ?