Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HorizontalScrollView with centered elements

Tags:

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| 

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> 
like image 980
Thierry-Dimitri Roy Avatar asked Mar 29 '11 20:03

Thierry-Dimitri Roy


1 Answers

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 !

like image 119
olamotte Avatar answered Nov 02 '22 21:11

olamotte