Domanda

is it possible to pass parameters to a included layout in android? What i would like to do for example is to display a set of buttons in a horizontal LinearLayout when in landscape mode, and to use the same include to change the orientation of the LinearLayout to "vertical".

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

In the layout XML used for portrait mode I would like to do:

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

And in buttons.xml I would have:

<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>

If I interpret the docs correctly it's only possible to override the layout* attributes of the include.

Is there an other way/workaround to do that without duplicating the layout xml? Thanks for your inputs!

È stato utile?

Soluzione 2

It's a bit late but here is another possibility without splitting the layout: you may also set the orientation value of the LinearLayout in an xml-file. Since the orientation values are integers (0: horizontal, 1: vertical) you may also use this way:

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

And then set/override this orientation value in xml-files:

dimens.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>

Altri suggerimenti

Android deals with this by providing diferent folders for the diferent configurations.

From documentation:

As another example, here's a project with an alternative layout for landscape orientation:

MyProject/

res/ layout/

       main.xml

   layout-land/

       main.xml

By default, the layout/main.xml file is used for portrait orientation.

I belive you should do it like that since it's the way recommended in docummentation, but you can change the orientation yourseft if you decide to control orientation changes.

And use this to change the layout orientation:

LinearLayout layout = (get layout from view);
layout.setOrientation(LinearLayout.VERTICAL);

or

layout.setOrientation(LinearLayout.HORIZONTAL); 

okay what I did in the end was to split my layout into 3 parts:

button_container.xml simply contains a LinearLayout and two additional includes

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:orientation="**horizontal**" >

   <include layout="@layout/button_top_row" />
   <include layout="@layout/button_bottom_row" />

</LinearLayout>
  • button_row_top contains the buttons that should go in the top row (in handheld port view)

-button_row_bottom contains the buttons that should go in the bottom row (again in handheld port view)

button_container.xml exists in two versions one with orientation horizontal (for tablets, and landsacpe oriented mobile handhelds) and a version with orientation vertical (for handheld port). So I managed to somehow minimise code douplication even though it isn't as easy as I would like it to be.

Any additional tips welcome. Bye, Markus

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top