gpt4 book ai didi

Android包含标签不覆盖布局属性

转载 作者:太空狗 更新时间:2023-10-29 12:46:06 24 4
gpt4 key购买 nike

我在使用 <include> 时遇到问题连同<merge> .我正在使用最新的 Android SDK (4.3) 在 Eclipse 上进行开发。这是我的示例代码:

test_merge.xml

<merge xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<View
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#FFFF0000" >
</View>

</merge>

test_main.xml

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

<include layout="@layout/test_merge"
android:layout_width="100dp"
android:layout_height="100dp"/>

</LinearLayout>

MyActivity.java

public class MyActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_main);
}
}

问题是我应该在左上角看到一个红色方 block ,100x100dp。相反,整个屏幕都是红色的。因此,由于某种原因 android:layout_widthandroid:layout_height <include> 中的属性没有效果。

我已经阅读了文档,它说为了通过 <include> 覆盖属性标签,我必须覆盖 android:layout_widthandroid:layout_height ,我已经做到了。

我做错了什么?

最佳答案

当使用 merge 时,没有要应用 100dp 的父对象。尝试切换到 FrameLayout 而不是 merge 或者(甚至更好)在这种情况下你可以只删除 merge 并拥有 View 成为根。

编辑:

将 test_merge.xml 更改为:

<View xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#FFFF0000" >
</View>

关于Android包含标签不覆盖布局属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18065894/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com