【问题标题】:textView and button wont fill screentextView 和按钮不会填满屏幕
【发布时间】:2015-03-27 18:53:40
【问题描述】:

我有一个相对布局设置为 match_parent,一个 textView 和按钮设置为 match_parent 宽度和 wrap_content 高度。

但在我的模拟器中,它们并没有填满整个屏幕宽度,如下图所示,它们周围有空白区域。

布局文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_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">

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="New Text"
    android:id="@+id/textView"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_above="@+id/button"
    android:background="#ff000000"
    android:textColor="#ff12ff13" />

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="New Button"
    android:id="@+id/button"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

【问题讨论】:

  • 请出示您的布局文件
  • 这个布局有什么问题? Button 和 textview 不会占据整个屏幕,因为您在 button 和 textview 上都使用了 wrap_content

标签: android android-layout


【解决方案1】:

你在根布局上有填充来填充屏幕使用这个:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" 
tools:context=".MainActivity">

【讨论】:

    【解决方案2】:

    您看到的空白是由于您在 RelativeLayout 中的填充。如果您希望填充填充屏幕,请删除它。

    【讨论】:

      【解决方案3】:

      从您的相关布局中删除以下 4 行

          android:paddingRight="@dimen/activity_horizontal_margin"
          android:paddingTop="@dimen/activity_vertical_margin"
          android:paddingBottom="@dimen/activity_vertical_margin"
      android:paddingLeft="@dimen/activity_horizontal_margin"
      

      【讨论】:

        【解决方案4】:

        请移除相关布局中的 layout_padding,如下所示:

        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
        android:layout_height="match_parent"  tools:context=".MainActivity">
        
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="New Text"
            android:id="@+id/textView"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:layout_above="@+id/button"
            android:background="#ff000000"
            android:textColor="#ff12ff13" />
        
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="New Button"
            android:id="@+id/button"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true" />
        </RelativeLayout>
        

        【讨论】:

          猜你喜欢
          • 2020-07-20
          • 1970-01-01
          • 1970-01-01
          • 2019-01-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-12-13
          • 1970-01-01
          相关资源
          最近更新 更多