gpt4 book ai didi

Android 在横向和纵向中重用布局

转载 作者:行者123 更新时间:2023-11-29 14:50:36 29 4
gpt4 key购买 nike

是否可以将参数传递给 android 中包含的布局?例如,我想做的是在横向模式下在水平 LinearLayout 中显示一组按钮,并使用相同的包含将 LinearLayout 的方向更改为“垂直”。

<include layout="buttons.xml" orientation="horizontal"/>

在用于纵向模式的布局 XML 中,我想做的是:

<include layout="buttons.xml" orientation="vertical"/>

在 buttons.xml 中我会:

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >

<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="doSomething"
android:text="foo" >
</Button>


<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="doSomethingElse"
android:text="bar" >
</Button>

如果我正确地解释文档,则只能覆盖包含的布局*属性。

是否有其他方法/解决方法可以在不复制布局 xml 的情况下做到这一点?感谢您的投入!

最佳答案

有点晚了,但还有另一种不拆分布局的可能性:您还可以在 xml 文件中设置 LinearLayout 的方向值。由于方向值是整数(0:水平,1:垂直),您也可以这样使用:

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="@integer/orientation" >

然后在 xml 文件中设置/覆盖这个方向值:

维度.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer name="orientation">1</integer> <!-- vertical //-->
</resources>

dimens-land.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer name="orientation">0</integer> <!-- horizontal //-->
</resources>

关于Android 在横向和纵向中重用布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19452896/

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