0

I am working with a layout that contains a LinerLayout and a TextView. When I set the gravity attribute to the TextView it stops showing on the device, yet shows up on the android layout preview.

Here is the xml

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

    <TextView
        android:id="@+id/txtProductName"
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_weight="0.1"
        android:background="@android:color/black"
        android:ellipsize="start"
        android:gravity="center"
        android:text="TextView"
        android:textColor="@android:color/white"
        android:textSize="20sp" />

    <LinearLayout
        android:id="@+id/grapher"
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="0.9"
        android:background="#000000"
        android:orientation="vertical" >
    </LinearLayout>

</LinearLayout>

Here is how it looks on the preview screen

Preview Screen

Yet on the device it stops showing

enter image description here

But when I remove the gravity attribute it shows up on the device. Why is this happening? How should I fix this?

4

2 回答 2

0

我使用线性布局作为父布局,它可以在模拟器和真实设备上运行。这是 xml:-->

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

<TextView
    android:id="@+id/txtProductName"
    android:layout_width="fill_parent"
    android:layout_height="0dip"
    android:layout_weight="0.1"
    android:background="@android:color/black"
    android:ellipsize="start"
    android:gravity="center"
    android:text="TextView"
    android:textColor="@android:color/white"
    android:textSize="20sp" />

<LinearLayout
    android:id="@+id/grapher"
    android:layout_width="match_parent"
    android:layout_height="0dip"
    android:layout_weight="0.9"
    android:background="#000000"
    android:orientation="vertical" >
</LinearLayout>

</LinearLayout>

请使用线性布局作为父级,如果有任何问题,请快速告诉我。

于 2013-11-08T09:06:15.440 回答
0
// try this
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center"
    android:padding="5dp"
    android:layout_width="match_parent">
    <TextView
        android:id="@+id/txtProductName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:color/black"
        android:ellipsize="start"
        android:text="TextView"
        android:textColor="@android:color/white"
        android:textSize="20sp" />

    <LinearLayout
        android:id="@+id/grapher"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:layout_marginTop="5dp"
        android:background="#000000"
        android:orientation="vertical" >
    </LinearLayout>
</LinearLayout>
于 2013-11-08T13:01:32.227 回答