如何创建方形灵活的图像按钮

时间:2014-02-07 06:11:07

标签: android android-layout

我需要为应用程序创建一个屏幕,其中屏幕的主要部分是4x4图像按钮。 我希望4个按钮将填充90%的屏幕宽度,每个按钮将是一个正方形。 我使用表格布局为按钮和layout_weight表格,使其适当宽度。如何使按钮的宽度和高度相同?

<TableLayout
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_gravity="center_horizontal"
    android:gravity="center_horizontal">

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" 
        android:layout_gravity="center_horizontal"
        android:gravity="center_horizontal">

        <ImageButton
            android:id="@+id/imageButton_1_1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="0.25"
            android:src="@android:drawable/btn_star" />

        <ImageButton
            android:id="@+id/imageButton_1_2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="0.25"
            android:src="@android:drawable/btn_star" />

        <ImageButton
            android:id="@+id/imageButton_1_3"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="0.25"
            android:src="@android:drawable/btn_star" />

        <ImageButton
            android:id="@+id/imageButton_1_4"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="0.25"
            android:src="@android:drawable/btn_star" />
        </TableRow>

...             

1 个答案:

答案 0 :(得分:0)

我想你知道如何把按钮填满90%的屏幕宽度,但你不知道如何设置高度,这样按钮就是方形,对吧?如果是这样,我认为你必须通过编码设置高度。第一步是获取按钮的宽度,然后设置它们的高度。