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.

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

尝试使用gravity而不是layout_gravity


尝试这个...

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_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="fill_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:layout_marginLeft="40dp"
    android:gravity="right" //////// HERE I AM 
    android:textStyle="bold"
    android:textColor="@color/item_text_color"/>


you could use weight also try this one

<?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"
android:orientation="horizontal" >

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

<TextView
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginLeft="8dp"
    android:text="TEST"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textStyle="bold" />

</LinearLayout>

the gravity is just to center the text ,what makes it go right is the weight

链接地址: http://www.djcxy.com/p/93322.html

上一篇: Android软键盘永远不会显示在模拟器中

下一篇: 重力不起作用