【问题标题】:Center TableLayout row in android xmlandroid xml中的中心TableLayout行
【发布时间】:2012-01-25 16:57:13
【问题描述】:

无论屏幕分辨率如何,我都试图在表格行的中心对齐 2 个按钮,因此我试图避免使用指定的边距。 这是我的代码,它只适用于声明的边距,我正在使用重力和布局重力......对我没有任何作用。他们没有在中心对齐。 有人有想法吗?还是谢谢。

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
         >

        <TableLayout
            android:id="@+id/TableLayout01"
            android:layout_gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="100px"
            android:stretchColumns="1" >

            <TableRow
                android:id="@+id/TableRow01"
                android:layout_gravity="center"
                android:paddingLeft="50dip"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >

                <Button
                    android:id="@+id/image1Btn"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@android:color/transparent"
                    android:drawablePadding="-5sp"
                    android:drawableTop="@drawable/image1"
                    android:gravity="center"
                    android:src="@drawable/image1"
                    android:text="@string/image1text"
                    android:textColor="@color/darkgrey" />

               <Button
                    android:id="@+id/image2Btn"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@android:color/transparent"
                    android:drawablePadding="-5sp"
                    android:drawableTop="@drawable/image2"
                    android:gravity="center"
                    android:src="@drawable/image2"
                    android:text="@string/image2text"
                    android:textColor="@color/darkgrey" />
            </TableRow>

            <TableRow
                android:id="@+id/TableRow02"
                android:paddingLeft="50dip"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >

               <Button
                    android:id="@+id/image3Btn"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@android:color/transparent"
                    android:drawablePadding="-5sp"
                    android:drawableTop="@drawable/image3"
                    android:gravity="center"
                    android:src="@drawable/image3"
                    android:text="@string/image3text"
                    android:textColor="@color/darkgrey" />

                <Button
                    android:id="@+id/image4Btn"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@android:color/transparent"
                    android:drawablePadding="-5sp"
                    android:drawableTop="@drawable/image4"
                    android:gravity="center"
                    android:src="@drawable/image4"
                    android:text="@string/image4text"
                    android:textColor="@color/darkgrey" />
            </TableRow>
        </TableLayout>
    </RelativeLayout>
</ScrollView>

【问题讨论】:

    标签: android center tablelayout


    【解决方案1】:

    将您的按钮放在几个 LinearLayouts 而不是 TableRow 中。

    <TableLayout
        ...
        >
    
        <LinearLayout
            android:orientation="vertical"
            >
    
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:layout_gravity="center"
                >
    
                <Button
                    android:id="@+id/image1Btn"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@android:color/transparent"
                    android:drawablePadding="-5sp"
                    android:drawableTop="@drawable/image1"
                    android:src="@drawable/image1"
                    android:text="@string/image1text"
                    android:textColor="@color/darkgrey" />
    
                <Button
                    android:id="@+id/image2Btn"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@android:color/transparent"
                    android:drawablePadding="-5sp"
                    android:drawableTop="@drawable/image2"
                    android:src="@drawable/image2"
                    android:text="@string/image2text"
                    android:textColor="@color/darkgrey" />
    
            </LinearLayout>
        </LinearLayout>
    
        ...
    </TableLayout>
    

    外部 LinearLayout 将被拉伸到 TableLayout 的宽度,并通过 TableLayout 将其高度设置为wrap_content。内部 LinearLayout 将在外部 LinearLayout 中水平居中,因为后者是垂直方向的。

    【讨论】:

    • 谢谢。如果我将 android:gravity="center_horizo​​ntal" 作为包含 TableLayouut 的 RelativeLayout 的属性,它会起作用。再次感谢:)
    【解决方案2】:
     <LinearLayout android:id="@+id/LinearLayout01" android:layout_width="fill_parent"  android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:background="@drawable/wood">
          <ScrollView android:id="@+id/ScrollView01" android:layout_width="fill_parent" android:layout_height="fill_parent">
               <AbsoluteLayout android:id="@+id/absoluteLayout1" android:layout_width="match_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:paddingLeft="10dp" android:paddingRight="10dp">
                     <TableLayout android:id="@+id/tableLayout0" android:background="@drawable/my_border" android:layout_width="fill_parent" android:layout_y="150dp" android:layout_height="match_parent">
                         <TableRow android:layout_height="wrap_content" android:id="@+id/tableRow1" android:layout_width="wrap_content">
                             <TextView android:id="@+id/EmailID" android:text="EmailID" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/my_border2" android:textStyle="bold" android:layout_weight="1"></TextView>
                             <EditText android:id="@+id/txtEmailID" android:layout_width="150dp" android:background="@drawable/my_border1" android:layout_height="wrap_content" android:layout_weight="1">
                                 <requestFocus></requestFocus>
                             </EditText>
                         </TableRow>
                         <TableRow android:layout_height="wrap_content" android:id="@+id/tableRow6" android:layout_width="wrap_content" android:paddingTop="20dp">
                             <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="Cancel"></Button>
                             <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="Login"></Button>
                         </TableRow>
                    </TableLayout>
    
             </AbsoluteLayout>  
          </ScrollView> 
      </LinearLayout>
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多