簡體   English   中英

如何均勻地對齊布局中的ImageButtons

[英]How to align ImageButtons in a layout evenly

我目前在布局中有4個ImageButton ,只占屏幕高度的一半。 我希望將ImageButton均勻地放置在布局中。 這是我有的:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <ImageButton
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:src="@mipmap/feed_button"
        android:background="@android:color/transparent"
        android:layout_weight="1" />

    <ImageButton
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:src="@mipmap/feed_button"
        android:background="@android:color/transparent"
        android:layout_weight="1" />

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="40dp"
    android:orientation="horizontal">

    <ImageButton
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:src="@mipmap/feed_button"
        android:background="@android:color/transparent"
        android:layout_weight="1" />

    <ImageButton
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:src="@mipmap/feed_button"
        android:background="@android:color/transparent"
        android:layout_weight="1" />

</LinearLayout>

</LinearLayout>

這是有效的,因為這是結果:

在此輸入圖像描述

問題是,1:我想在每個按鈕下添加文本,我不確定它是如何工作的,2: ImageButton的左右兩邊的白點是活動的,好像它們是按鈕。 有一個更好的方法嗎?

你可以做這樣的事情

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_gravity="center"
    android:layout_weight="1"
    android:gravity="center"
    android:orientation="horizontal">

    <RelativeLayout
        android:id="@+id/section1"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@android:color/transparent">

        <ImageButton
            android:id="@+id/img1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:layout_weight="1"
            android:background="@android:color/transparent"
            android:src="@mipmap/ic_launcher" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/img1"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="10dp"
            android:text="info 1"
            android:textColor="#fff"
            android:textSize="25sp" />
    </RelativeLayout>


    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@android:color/transparent">

        <ImageButton
            android:id="@+id/img2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:layout_weight="1"
            android:background="@android:color/transparent"
            android:src="@mipmap/ic_launcher" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/img2"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="10dp"
            android:text="info 2"
            android:textColor="#fff"
            android:textSize="25sp" />
    </RelativeLayout>


</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_gravity="center"
    android:layout_weight="1"
    android:gravity="center"
    android:orientation="horizontal">

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@android:color/transparent">

        <ImageButton
            android:id="@+id/img3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:layout_weight="1"
            android:background="@android:color/transparent"
            android:src="@mipmap/ic_launcher" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/img3"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="10dp"
            android:text="info 3"
            android:textColor="#fff"
            android:textSize="25sp" />
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@android:color/transparent">

        <ImageButton
            android:id="@+id/img4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:layout_weight="1"
            android:background="@android:color/transparent"
            android:src="@mipmap/ic_launcher" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/img4"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="10dp"
            android:text="info 4"
            android:textColor="#fff"
            android:textSize="25sp" />
    </RelativeLayout>

</LinearLayout>

結果

在此輸入圖像描述

對於第二部分問題,您應該在整個相對布局上設置單擊偵聽器。 例如,點擊第一個單元格: -

XML

 android:id="@+id/section1"

JAVA

findviewbyid(R.id.section1).setOnClickListener(...)

另一種(更好的)替代方法是使用TableLayout:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:stretchColumns="1">

    <TableRow
        android:layout_height="0dp"
        android:layout_weight="1">

        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@android:color/transparent">

            <ImageButton
                android:id="@+id/btn1"
                android:contentDescription="@null"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:src="@mipmap/feed_button"
                android:background="@android:color/transparent"/>

            <TextView
                android:id="@+id/txt1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/btn1"
                android:layout_marginTop="10dp"
                android:layout_centerHorizontal="true"
                android:text="Text 1"/>

        </RelativeLayout>

        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@android:color/transparent">

            <ImageButton
                android:id="@+id/btn2"
                android:contentDescription="@null"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:src="@mipmap/feed_button"
                android:background="@android:color/transparent"/>

            <TextView
                android:id="@+id/txt2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/btn2"
                android:layout_marginTop="10dp"
                android:layout_centerHorizontal="true"
                android:text="Text 2"/>

        </RelativeLayout>
    </TableRow>

    <TableRow
        android:layout_height="0dp"
        android:layout_weight="1">

        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@android:color/transparent">

            <ImageButton
                android:id="@+id/btn3"
                android:contentDescription="@null"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:src="@mipmap/feed_button"
                android:background="@android:color/transparent"/>

            <TextView
                android:id="@+id/txt3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/btn3"
                android:layout_marginTop="10dp"
                android:layout_centerHorizontal="true"
                android:text="Text 3"/>

        </RelativeLayout>

        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@android:color/transparent">

            <ImageButton
                android:id="@+id/btn4"
                android:contentDescription="@null"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:src="@mipmap/feed_button"
                android:background="@android:color/transparent"/>

            <TextView
                android:id="@+id/txt4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/btn4"
                android:layout_marginTop="10dp"
                android:layout_centerHorizontal="true"
                android:text="Text 4"/>

        </RelativeLayout>

    </TableRow>

</TableLayout>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM