2010-11-25 2 views
8

Как я могу центрировать эти кнопки на Android? alt textРасположение центра Android

код, я использую в layout является:

<?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"> 

     <TextView android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/hello" /> 

     <LinearLayout android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:orientation="horizontal"> 

      <Button android:text="Test" 
       android:layout_width="100px" 
       android:layout_height="40px" /> 
      <Button android:text="Test" 
       android:layout_width="100px" 
       android:layout_height="40px" /> 
      <Button android:text="Test" 
       android:layout_width="100px" 
       android:layout_height="40px" /> 
      <Button android:text="Test" 
       android:layout_width="100px" 
       android:layout_height="40px" /> 
     </LinearLayout> 
</LinearLayout> 

ответ

15

Сделайте LinearLayout, содержащий кнопки, чтобы wrap_content по ширине и иметь center_horizontal по гравитации.

<?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"> 

<TextView android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/hello" /> 

<LinearLayout android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:orientation="horizontal" 
     android:layout_gravity="center_horizontal"> 
    <Button android:text="Test" 
     android:layout_width="100px" 
     android:layout_height="40px" /> 
    <Button android:text="Test" 
     android:layout_width="100px" 
     android:layout_height="40px" /> 
    <Button android:text="Test" 
     android:layout_width="100px" 
     android:layout_height="40px" /> 
    <Button android:text="Test" 
     android:layout_width="100px" 
     android:layout_height="40px" /> 
     </LinearLayout> 
</LinearLayout> 
+1

Отлично. Благодаря! – 2010-11-25 18:01:09

1

Я хотел бы знать, если кто-то имеет какие-либо комментарии по этому поводу, а также. Я всегда делал это с помощью TableLayout и устанавливал как stretchColumns, так и collapseColumns для всех столбцов, поэтому он масштабируется на всех размерах экрана. Немного вот так:

<TableLayout 
    android:layout_width="fill_parent" 
    android:layout_height="47dp" 
    android:stretchColumns="*" 
    android:collapseColumns="*" 
    android:padding="0px" 
    android:background="@drawable/bottom_bg"> 
    <TableRow> 
    <ImageView 
     android:id="@+id/article_button_home" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/button_home_selector" 
     android:layout_centerHorizontal="true" /> 
    <ImageView 
     android:id="@+id/article_button_share" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/button_sharearticle_selector" 
     android:layout_centerHorizontal="true" /> 
    <ImageView 
     android:id="@+id/article_button_settings" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/button_settings_selector" 
     android:layout_centerHorizontal="true"/> 
    <ImageView 
     android:id="@+id/article_button_about" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true"  
     android:src="@drawable/button_about_selector"/> 
</TableRow> 
</TableLayout> 

Мне кажется, что это лучший способ.

Смежные вопросы