Home » Android » android – layout "include" feature doesn't support layout_gravity property?

android – layout "include" feature doesn't support layout_gravity property?

Posted by: admin June 15, 2020 Leave a comment

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

    <include
        layout="@layout/view1" 
        android:layout_gravity="center_vertical" />

    <include
        layout="@layout/view2"
        android:layout_gravity="center_vertical" />
</LinearLayout>

So, android:layout_gravity will be always ignored? That’s really bad for code reusability…
It seems like layout_margin doesn’t supported either.

And here is view1.xml layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:minHeight="45dip"
    android:background="@drawable/updater_background" >

    <ProgressBar
        style="?android:attr/progressBarStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_marginRight="7dip"
        android:visibility="gone" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical" />
</LinearLayout>
How to&Answers:

Two years later, the bug still exists ! Even though ALL android:layout_* attributes should be supported by the tag, the android:layout_gravity attribute is not (which kind of is a big disadvantage for layout re-usability).

The work around that I am currently using is to set the layout margin programmatically as follows :

( (LinearLayout.LayoutParams) includedView.getLayoutParams () ).gravity = Gravity.BOTTOM | Gravity.RIGHT;

OR for a FrameLayout as parent for example :

( (FrameLayout.LayoutParams) includedView.getLayoutParams () ).gravity = Gravity.BOTTOM | Gravity.RIGHT;

EDIT :

I have also found that the android:layout_gravity attribute is applied for the include tag IF it (The include tag) has layout_width and layout_height tags !

Answer:

I think this can be solved by inserting weighted layout

For example

<LinearLayout
  android:orientation="vertical"
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context="com.sample.SampleActivity">

 <LinearLayout
    android:layout_weight="1"
    android:layout_width="match_parent"
    android:layout_height="0dp">

    <--Some other Views-->

</LinearLayout>


<include
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    layout="@layout/my_awesome_layout"
    />

The LinearLayout can be replaced with Space, if you don’t want any component.

Answer:

http://developer.android.com/resources/articles/layout-tricks-reuse.html states all the layout_ attributes are supported.

In your case centering in a horizontal LinearLayout probably has no effect. Try another layout other parameters.