相对布局不填充父母与背景颜色
问题描述:
我想用相对布局填充父母与黑色背景,但它不工作。相对布局不填充父母与背景颜色
这是代码:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000"
android:orientation="vertical">
<!-- this should fill the parent with black but its not filling -->
</RelativeLayout>
</LinearLayout>
</ScrollView>
答
将这个代码
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:fillViewport="true"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000"
android:orientation="vertical">
<!-- this should fill the parent with black but its not filling -->
</RelativeLayout>
</LinearLayout>
</ScrollView>
答
你正在做几件事情错了:
如果你想整个滚动型有黑色的背景只是将其设置在ScrollView本身上并将其设置为使用fillViewport填充整个视图
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:background="#000" >
个
RelativeLayouts没有方向,这是一个的LinearLayout属性
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000"
android:orientation="vertical"> <--- WRONG!
当您使用滚动型的,你应该给它一个整体布局的孩子,无论是直线还是相对的,所以,你必须在里面的元素更多的控制权ScollView。如果在LinearLayout中只有一个RelativeLayout,则只需使用RelativeLayout。
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:background="#000" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Your elements -->
</RelativeLayout>
</ScrollView>
如果你给你的RelativeLayout匹配父项,你为什么要有LinearLayout? –
并在相对布局中添加一些调色板,然后它会在预览中显示背景颜色。 –
删除线性布局产生相同的结果 – user6731422