Android setBackgroundColor(内部颜色)和RelativeLayout
当我使用这个XML文件时Android setBackgroundColor(内部颜色)和RelativeLayout,android,android-relativelayout,background-color,Android,Android Relativelayout,Background Color,当我使用这个XML文件时 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/RelativeLayout1" android:layout_width="match_parent" android:layout_height="match_parent" andro
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF0000"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</RelativeLayout>
…我看到背景颜色与父级匹配,而不是包裹内容
有什么想法吗
谢谢 您从未设置布局参数:
layout.setLayoutParams(params);
我认为你应该把:
TextView text = new TextView(this);
text.setText("Hello world");
在设置布局参数之前,您将添加到布局中的所有其他视图,否则将没有内容可环绕
编辑:
设置以下选项时会发生什么:
tried to replace between the two:
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
?我得到了同样的结果。。。我不明白为什么…因为我认为我的代码和XML文件是相同的=我忘记了“layout.setLayoutParams”!现在我尝试将textview添加到RelativeLayout,然后将params添加到RelativeLayout,但得到了相同的结果:相同,所有屏幕都是红色的。我认为这没有任何效果,因为我尝试使用300px的宽度和高度,我也看到了所有的屏幕红色:如果我使用LinearLayout,然后我将relativelayout(及其参数和textview)添加到LinearLayout,它的工作原理与XML文件非常相似。你明白为什么吗?我想如果你的相对布局是你的根视图,那么布局参数就不同了。
tried to replace between the two:
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);