Android 在parentview的一半中居中显示图像视图,而不使用LinearLayout

Android 在parentview的一半中居中显示图像视图,而不使用LinearLayout,android,android-layout,imageview,android-linearlayout,android-relativelayout,Android,Android Layout,Imageview,Android Linearlayout,Android Relativelayout,目前我的布局设计方式是,我有一个LinearLayout和一个width=“match\u parent”。此视图有两个图像视图作为其子视图,使用weight=“1” 问题是,这样一来,onclick就会注册到卡的透明部分,因为视图被拉伸了 编辑:我应该提到的是,这些圆应该只代表图像文件,而不是用纯色填充的圆形状 我当前的代码: <LinearLayout android:layout_width="match_parent" android:layout_height="

目前我的布局设计方式是,我有一个
LinearLayout
和一个
width=“match\u parent”
。此视图有两个图像视图作为其子视图,使用
weight=“1”

问题是,这样一来,onclick就会注册到卡的透明部分,因为视图被拉伸了

编辑:我应该提到的是,这些圆应该只代表图像文件,而不是用纯色填充的圆形状

我当前的代码:

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

    <ImageView

        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" />

    <ImageView

        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" />

</LinearLayout>

红色部分表示触摸区域

我想要的是在保持比率不变的情况下如下所示:

我知道我可以简单地尝试将每个ImageView嵌套在一个RelativeLayout中,如下所示:

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

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1">

        <ImageView
            android:layout_centerInParent="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" />

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1">

        <ImageView
            android:layout_centerInParent="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </RelativeLayout>


</LinearLayout>


但是像这样的嵌套是非常可怕的形式

你可以使用RelativeLayout

<RelativeLayout
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1">

    <ImageView
        android:layout_alignParentLeft="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <ImageView
        android:layout_alignParentRight="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</RelativeLayout>


但是,如果屏幕的纵横比较低,有时会导致重叠。

您可以看到被触摸像素的颜色,并相应地采取行动:

 final Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
    imageView.setOnTouchListener(new OnTouchListener(){
        @Override
        public boolean onTouch(View v, MotionEvent event){
        int x = (int)event.getX();
        int y = (int)event.getY();
        int pixel = bitmap.getPixel(x,y);

        int redValue = Color.red(pixel);
        int blueValue = Color.blue(pixel);
        int greenValue = Color.green(pixel); 
        if(redValue != 255 && blueValue != 255 && greenValue != 255) {
            //Not the background, so handle the click
        }
        return false;
        }
   });

尝试创建新组件:
res/drawable/my_circle.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="@color/your_color" />
</shape>

使用百分比相对布局无需嵌套

使用百分比相对布局无需嵌套@巴厘岛,效果很好,谢谢!如果您提交您的评论作为答案,我将标记为这样!我添加了我的答案作为评论,请检查这不是op想要的效果,我最终使用了带有
width=“match_parent”
的PercentageRelativeLayout,并将
marginLeftPercentage
marginRightPercentage
分配为
17%
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <ImageView

        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:src="@drawable/my_circle" />

    <ImageView

        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:src="@drawable/my_circle" />

</LinearLayout>