android - 如何在包含 TextView 的android中更改整个线性布局的背景颜色

标签 android android-layout

我有一个正在处理的项目,我有一个包含两个 TextView 的 LinearLayout。我已经使用 android:background = "@color/..."设置了 LinearLayout 的背景颜色,但是这只适用于 TextView 结束的地方。如果它在屏幕中间结束,那么颜色就停在那里,其余的向下显示白色。 我不明白。我已经彻底搜索无济于事。 请大家帮帮我。这是我的布局文件。

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/gold">

<TextView
  android:id="@+id/textView1"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="@color/gold"
  android:paddingBottom="10dp"
  android:paddingLeft="15dp"
  android:paddingTop="10dp"
  android:textSize="20sp" />

 <TextView
  android:id="@+id/textView2"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="@color/gold"
  android:paddingLeft="15dp"
  android:paddingRight="10dp"
  android:textSize="15sp" />

 </LinearLayout>

问候。

最佳答案

您的 LinearLayout 可能只使用 wrap_content。如果您希望全屏显示该颜色,您的 LinearLayout 必须扩展以使用全屏:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/my_color>

</LinearLayout>

如果您的 LinearLayout 不是布局中的根元素,则将背景设置为根元素,确保它的宽度和高度都具有 match_parent。

关于android - 如何在包含 TextView 的android中更改整个线性布局的背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35803423/

相关文章:

R8 的 Android 构建失败

android - 宽度小于 match_parent 的水平回收器 View

android - Edittext 在 Recyclerview 和 NestedScrollView android 中滚动

android - 如何使大图像可点击不同的不同图像部分

android - 自定义 View 不填满屏幕?

具有灵活高度的ListView中的Android自定义行布局

java - 两个日期之间的差异?

android - Eclipse 中的 Joda 时间 : NoClassDefFoundError

Android RecyclerView 在加载数据时滚动到顶部

java - 如何让 float View 适合 TextView 数量可变的多行?