8

如何在 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>
4

2 回答 2

15

使包含按钮的 LinearLayout 在宽度上 wrap_content 并在重力上具有 center_horizo​​ntal 。

<?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>
于 2010-11-25T17:55:33.567 回答
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>

我确实想知道这是否是最好的方法。

于 2010-11-25T17:47:39.967 回答