将前景色和中心图标添加到可单击视图中

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

我正在努力实现这样的目标! Alternative to check box我如何实现这一目标?

我尝试使用中央图标和透明背景创建一个drawable,但是当drawable设置为视图前景时,图标会缩放以填充视图。

对于小于23的api级别,也不支持setForground方法

我打算在单击视图时动态实现此功能。请帮忙!!!

android android-layout click android-drawable
2个回答
1
投票

实现此视图使用相似的布局,如下面的代码

<?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:gravity="center"
android:orientation="vertical">


<RelativeLayout
    android:layout_width="90dp"
    android:layout_height="120dp">

    <ImageView
         android:id="@+id/image_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/demo_img_4" />

    <LinearLayout
        android:id="@+id/transp_ll"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#30000000"
        android:gravity="center">

        <ImageView
             android:id="@+id/delete_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_delete_black_24dp" />
    </LinearLayout>
</RelativeLayout>
</LinearLayout>

main activity.Java

  @Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //initialize views
    setContentView(R.layout.activity_main);
    image_view = findViewById(R.id.image_view);
    delete_btn = findViewById(R.id.delete_btn);
    transp_ll = findViewById(R.id.transp_ll);

    //set transparent layout visibility gone
    transp_ll.setVisibility(View.GONE);

    //set click listener
    image_view.setOnClickListener(this);
    delete_btn.setOnClickListener(this);
}

@Override
public void onClick(View view) {
    switch (view.getId()) {
        case R.id.delete_btn:
            //write here delete button click functionality..
            transp_ll.setVisibility(View.GONE);
            break;
        case R.id.image_view:
            transp_ll.setVisibility(View.VISIBLE);
            break;
    }
}

enter image description here enter image description here


0
投票

您应该添加一个与您查看的父级相匹配的透明视图,并在其中心添加一个图像视图。您可以调整透明度级别以获得所需的效果

© www.soinside.com 2019 - 2024. All rights reserved.