问题描述
我开始使用Android Studio,创建了一个小应用并设置了背景:
android:background="#3F51B5"
但是当我运行模拟器时,我看到的是较低的白/暗带,特别是在具有较大显示屏的智能手机中
我该怎么办?
我使用线性布局和带有约束布局的一节。主要活动的第一部分是
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="false"
android:background="#3F51B5"
android:orientation="vertical"
android:paddingLeft="16dp"
android:paddingRight="16dp"
tools:context=".MainActivity"
tools:visibility="visible">
解决方法
这样做:
android:layout_width="match_parent"
android:layout_height="match_parent"
通过这种方式,您可以使用所选颜色填充所有显示内容
,如果您希望将LinearLayout
保持原样,而将整个背景保留为所需的颜色,那么我建议将其包装在ConstraintLayout
中,其宽度和长度可以匹配父级,或者高度可以匹配根据您的需要包装内容
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#3F51B5">
<LinearLayout
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="false"
android:orientation="vertical"
android:paddingLeft="16dp"
android:paddingRight="16dp"
tools:context=".MainActivity"
tools:visibility="visible">
//Views
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>