代码之家  ›  专栏  ›  技术社区  ›  ingh.am

安卓中心布局

  •  8
  • ingh.am  · 技术社区  · 15 年前

    我怎样才能把这些按钮放在Android上? alt text

    我正在使用的代码 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>
    
    2 回复  |  直到 11 年前
        1
  •  15
  •   aromero    15 年前

    使包含按钮的线性布局在宽度上包裹内容并在重力上使中心水平。

    <?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>
    
        2
  •  1
  •   GeekYouUp    15 年前

    我想知道其他人对此是否也有意见。我总是使用一个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>
    

    不过,我想知道这是不是最好的办法。

    推荐文章