GridLayout中的对象对齐方式

时间:2014-09-02 11:06:41

标签: android textview android-xml android-textattributes

我有一个这样的xml标记:

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

    <GridLayout
            android:layout_width="match_parent"
            android:layout_height="fill_parent">
        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:text="13:40"
                android:id="@+id/textView" android:layout_marginLeft="10dp"/>
        <LinearLayout
                android:orientation="vertical"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp">
            <TextView
                    android:id="@+id/nameText"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:text="Large Text"
                    android:textAppearance="?android:attr/textAppearanceLarge" android:layout_row="0"
                    android:layout_column="0"/>
            <TextView
                    android:id="@+id/originalText"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:text="Medium Text"
                    android:textAppearance="?android:attr/textAppearanceMedium" android:layout_row="1"
                    />
        </LinearLayout>
        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="right"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:text="150-220"
                android:id="@+id/textView2" android:layout_marginRight="10dp"/>
    </GridLayout>

</LinearLayout>

在IDE中它看起来很好:

enter image description here

但是当在模拟器中运行应用程序右翼textView2时没有与右边对齐,尽管我在标记中有它:

enter image description here

我做错了什么?

感谢。

1 个答案:

答案 0 :(得分:0)

请尝试这种方式,希望这有助于您解决问题。

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

    <GridLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:columnCount="4">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="13:40"
            android:layout_marginLeft="10dp"
            android:layout_column="0"
            android:layout_row="0"/>
        <LinearLayout
            android:orientation="vertical"
            android:layout_marginLeft="10dp"
            android:layout_column="1"
            android:layout_columnSpan="2"
            android:layout_row="0">
            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="Large Text"
                android:textAppearance="?android:attr/textAppearanceLarge"/>
            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="Medium Text"
                android:textAppearance="?android:attr/textAppearanceMedium"/>
        </LinearLayout>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="150-220"
            android:layout_marginRight="10dp"
            android:layout_row="0"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="13:40"
            android:id="@+id/textView"
            android:layout_marginLeft="10dp"
            android:layout_column="0"
            android:layout_row="1"/>
        <LinearLayout
            android:orientation="vertical"
            android:layout_marginLeft="10dp"
            android:layout_column="1"
            android:layout_columnSpan="2"
            android:layout_row="1">
            <TextView
                android:id="@+id/nameText"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="Large Text"
                android:textAppearance="?android:attr/textAppearanceLarge"/>
            <TextView
                android:id="@+id/originalText"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="Medium Text"
                android:textAppearance="?android:attr/textAppearanceMedium"/>
        </LinearLayout>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="150-220"
            android:id="@+id/textView2"
            android:layout_marginRight="10dp"
            android:layout_row="1"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="13:40"
            android:layout_marginLeft="10dp"
            android:layout_column="0"
            android:layout_row="2"/>
        <LinearLayout
            android:orientation="vertical"
            android:layout_marginLeft="10dp"
            android:layout_column="1"
            android:layout_columnSpan="2"
            android:layout_row="2">
            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="Large Text"
                android:textAppearance="?android:attr/textAppearanceLarge"/>
            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="Medium Text"
                android:textAppearance="?android:attr/textAppearanceMedium"/>
        </LinearLayout>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="150-220"
            android:layout_marginRight="10dp"
            android:layout_row="2"/>
    </GridLayout>

</LinearLayout>