Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

layout_gravity not working

Why my TextView doesn't go right?

Update: Well, now I don't just need to set TextView to the right. Now it is very interesting why layout_gravity doesn't work as expected, namely - set the View to the position inside it parent container.

enter image description here

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:minHeight="?android:attr/listPreferredItemHeight"
              style="@style/activated_item"
              android:orientation="horizontal">

    <CheckBox
        android:id="@+id/star"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginLeft="8dp"
        style="?android:attr/starStyle"/>

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:layout_marginLeft="8dp"
        android:layout_gravity="right" //////// HERE I AM 
        android:textStyle="bold"
        android:textColor="@color/item_text_color"/>

</LinearLayout>
like image 942
Eugene Avatar asked Aug 29 '12 19:08

Eugene


People also ask

What is true about layout_gravity attribute in Linearlayout?

The layout_gravity attribute can be used to position the Button as a whole with the Button text centered in side it. The gravity attribute can be used to position the text inside the Button. See the screenshot below showing the use of the layout_gravity attribute and the gravity attribute for the Button widget.

What is the difference between android gravity and android layout_ gravity?

android:gravity sets the gravity of the contents (i.e. its subviews) of the View it's used on. android:layout_gravity sets the gravity of the View or Layout relative to its parent.

What is android gravity?

android:gravity is an attribute that sets the gravity of the content of the view its used on. The android:gravity specifies how an object should position its content on both X and Y axis. The possible values of android:gravity are top, bottom, left, right, center, center_vertical, center_horizontal etc.


3 Answers

I have found 2 solutions to that:

  1. Set android:orientation="vertical".
  2. Use FrameLayout instead of LinearLayout as the parent for your text/checkbox widgets. It is not primary usage of FrameLayout but that advice is also noted in Android documentation:

    ... You can, however, add multiple children to a FrameLayout and control their position within the FrameLayout by assigning gravity to each child, using the android:layout_gravity attribute.

like image 173
dpetruha Avatar answered Nov 01 '22 16:11

dpetruha


Try to use gravity instead of layout_gravity.

like image 33
poitroae Avatar answered Nov 01 '22 17:11

poitroae


Because you set orientation to horizontal, which means you cannot manually change horizontal position of your child views.

like image 35
Sam Chen Avatar answered Nov 01 '22 17:11

Sam Chen