增加ImageView的可点击区域

问题描述 投票:0回答:2

我已经创建了下面显示的RelativeLayout作为工具栏的一部分。布局由两个图像和图像下方的细线组成。对于这两个图像,我增加了宽度,因为可单击区域变宽了。屏幕快照(Android Studio)的外观显示在底部。

现在的问题是我无法控制图像的大小(图像只是放置在中间)。如何将图像的实际大小固定为25 x 25 dp,并且仍将可点击区域扩展到图像的左右两侧(例如70 dp),并在图像的底部和顶部扩展10 dp?

<RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/keyboard_toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    android:orientation="horizontal">

    <View
        android:id="@+id/keyboard_toolbar_line"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_below="@id/privacyButton"
        android:layout_marginBottom="5dp"
        android:background="#c0c0c0"/>

    <ImageView
        android:id="@+id/privacyButton"
        android:layout_width="100dp"
        android:layout_alignParentStart="true"
        android:layout_height="35dp"
        android:scaleType="center"
        android:layout_marginStart="30dp"
        android:src="@drawable/research_ic_lock_open_dark" />

    <ImageView
        android:id="@+id/ratingButton"
        android:layout_width="100dp"
        android:layout_alignParentEnd="true"
        android:layout_height="35dp"
        android:scaleType="center"
        android:layout_marginEnd="30dp"
        android:src="@drawable/ic_star" />

</RelativeLayout >

enter image description here

android android-imageview android-relativelayout
2个回答
0
投票

[您可能会考虑将其包装在具有所需宽度和高度的LinearLayout中,并将图像保持在25x25dp;然后将点击监听器添加到LinearLayout而不是ImageView

一张图像的样本:

<LinearLayout
    android:layout_width="140dp"
    android:gravity="center"
    android:layout_height="35dp">

    <ImageView
        android:id="@+id/privacyButton"
        android:layout_width="25dp"
        android:layout_height="25dp"
        android:layout_alignParentStart="true"
        android:layout_marginStart="30dp"
        android:scaleType="center"
        android:src="@drawable/quarter_circle" />

</LinearLayout>

我只是为LinearLayout的宽度添加了任意值,因为您需要左右70dp,所以总数为140。


0
投票

如果我没记错的话,这是您的解决方案。

您首先放置一个父布局,然后在其中放置ImageView。然后在父级上单击onclicklistener

 <RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/keyboard_toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:orientation="horizontal">

        <View
            android:id="@+id/keyboard_toolbar_line"
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_below="@id/privacyButton"
            android:layout_marginBottom="5dp"
            android:background="#c0c0c0"/>

        <RelativeLayout
            android:layout_width="100dp"
            android:layout_height="35dp">
            <ImageView
                android:id="@+id/privacyButton"
                android:layout_width="35dp"
                android:layout_alignParentStart="true"
                android:layout_height="35dp"
                android:scaleType="center"
                android:layout_marginStart="30dp"
                android:src="@drawable/research_ic_lock_open_dark" />
        </RelativeLayout>
    <RelativeLayout
        android:layout_width="100dp"
        android:layout_height="35dp">

        <ImageView
            android:id="@+id/ratingButton"
            android:layout_width="35dp"
            android:layout_alignParentEnd="true"
            android:layout_height="35dp"
            android:scaleType="center"
            android:layout_marginEnd="30dp"
            android:src="@drawable/ic_star" />
    </RelativeLayout>

    </RelativeLayout>
© www.soinside.com 2019 - 2024. All rights reserved.