使用引用而不是layout-land(仅xml)设置LinearLayout方向

gia*_*200 5 android android-layout android-linearlayout android-xml android-resources

我有一个用于横向和纵向的布局.我有它/layout/layout-land文件夹.唯一的区别是android:orientation.

我想只有1 xml,/layout因为它更容易维护.我想使用/values/values-land不是.这可能吗?

我现在拥有的是(删除了不必要的代码):

<LinearLayout 
    android:orientation="horizontal"/>
Run Code Online (Sandbox Code Playgroud)

VS

<LinearLayout 
    android:orientation="vertical"/>
Run Code Online (Sandbox Code Playgroud)

我虽然喜欢这样的东西:

<LinearLayout 
    android:orientation="@xxxx/linear_orientation"/>
Run Code Online (Sandbox Code Playgroud)

作为一个终极解决方案,我还考虑过仅为此创建一个自定义样式LinearLayout(我不喜欢这个解决方案,但如果我不能使用另一个选项则必须使用它).

注意:我知道可以这样做java,但我正在寻找一个只有xml的解决方案.

Lei*_*Guo 13

我想在/ layout中只有1 xml,因为它更容易维护.我想改用/ values和/ values-land.这可能吗?

是的,这是可能的.您可以使用Style来实现这一目标.

/ values下styles.xml中的Style:

<style name="LinearLayoutOrientation" >
    <item name="android:orientation">vertical</item>
</style>
Run Code Online (Sandbox Code Playgroud)

/ values-land下styles.xml中的Style:

<style name="LinearLayoutOrientation" >
    <item name="android:orientation">horizontal</item>
</style>
Run Code Online (Sandbox Code Playgroud)

LinearLayout的最终xml代码:

 <LinearLayout 
    style="@style/LinearLayoutOrientation"/>
Run Code Online (Sandbox Code Playgroud)