开发者

Android SDK Align two elements in center

Im trying to align two elements in my xml file. I want to display a TextView called "Description" and a TextView called "Value" on the same line. The Description should right aligned to the center, the Value should be left aligned the center. But the following gravity doesnt seem to affect them.

I assume I should avoid using AbsoluteLayout because of different screen sizes.

<开发者_Python百科?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:id="@+id/LinearLayout01"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/TextViewDescription"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:text="@+id/TextView01" >
    </TextView>

    <TextView
        android:id="@+id/TextViewValue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="left"
        android:text="@+id/TextView02" >
    </TextView>

</LinearLayout>

I also looked at TableLayout but struggled with that too.

What is the correct way to create a set of description/values down the screen that are center aligned so they appear fine on both portrait and landscape view?


Try this:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout01"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ImageView
        android:id="@+id/dummy"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true" />

    <TextView
        android:id="@+id/TextViewValue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@id/dummy"
        android:text="@+id/TextView01" />

    <TextView
        android:id="@+id/TextViewDescription"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/dummy"
        android:text="@+id/TextView02" />

</RelativeLayout>


Use android:gravity="center" in a TableRow.

The following ideas are not elegant. Just for reference.

Use RelativeLayout. Create an invisible control as Layout centerHorizontal. Then use Layout toLeftOf and toRightOf to this control to make the controls to the left or right to the center. Maybe the "control" must be a layout.

Another way, to use TableLayout inside a RelativeLayout, if your controls are measured in dip's. Use center_horizontal in RelativeLayout, and specify TableLayout's width just big enough for the two controls. Then put the controls in a TableRow.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜