开发者

HorizontalScrollView with centered elements

I have a HorizontalScrollView that is filled dynamically with TextViews. Sometime there is one, two, three or more elements. What I'm looking for is a way to have the items centered regardless of the numbers of elements.

For example, With one element:

| --------- MenuA --------- |

With two elements:

| ---- MenuA --- MenuB ---- |

With three elements:

| - MenuA - MenuB - MenuC - |

With five elements (MenuD and MenuE are hidden):

| MenuA - MenuB - MenuC - Me|

开发者_如何学C

Update: Here is a simple layout with two menus. How do I center MenuA and MenuB?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <HorizontalScrollView android:id="@+id/horizontalScrollView1"
        android:layout_width="wrap_content" android:layout_height="wrap_content">
        <LinearLayout android:id="@+id/linearLayout1"
            android:layout_width="fill_parent" android:layout_height="fill_parent"
            android:orientation="horizontal">
            <TextView android:text="MenuA" android:id="@+id/textView1"
                android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
            <TextView android:text="MenuB" android:id="@+id/textView2"
                android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
        </LinearLayout>
    </HorizontalScrollView>
</LinearLayout>


use the gravity attribute :

android:layout_gravity="center_horizontal" EDIT : 03.30 :

I found it ! only had to set the gravity to the upper LinearLayout : all you need to do is add a little padding/margins to make the textviews more comfortable to read !

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

    <HorizontalScrollView
        android:id="@+id/horizontalScrollView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal" >

        <LinearLayout
            android:id="@+id/linearLayout1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="horizontal" >

            <TextView
                android:id="@+id/TextView04"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="MenuB" >
            </TextView>

            <TextView
                android:id="@+id/TextView03"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="MenuB" >
            </TextView>

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

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

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="MenuA" >
            </TextView>

            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="MenuB" >
            </TextView>
        </LinearLayout>
    </HorizontalScrollView>

</LinearLayout>

keep me posted !

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜