简体   繁体   中英

Android layout include no layout

In my app, portrait would have a Button where landscape would NOT have a Button , so I use <include> and did this:

layout/activity_main.xml

...
<include layout="@layout/my_button" />
...

values/layout.xml

...
<item name="my_button" type="layout">@layout/my_button_layout</item>
...

values-land/layout.xml

....
<item name="my_button" type="layout">@layout/empty_layout</item>

layout/my_button_layout.xml

...
<Button...... <!-- my button XML -->
...

layout/empty_layout.xml

<View xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone" />

Now, this works, but I don't quite like the idea of having an useless view whenever the app's in landscape.

I tried to do:

<item name="my_button" type="layout">@null</item>

but this would throw a ResourceNotFound Exception

Is there a way around this?

(Yes I know I can do this programmatically, but then it defeats the purpose of Android's layout system)

You could use two styles. One in an xml file in a portrait folder and the other in another xml file in the landscape folder. These styles could then set android:visibility="gone" and android:visibility="visible" then just set the style onto the button using style="@style/MyButtonStyle"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM