运行时ImageButton与PNG图像透明背景失败

时间:2017-01-15 22:15:54

标签: android drawable imagebutton

我想在每次按下ADD按钮(参见红色圆圈)时添加2个Edittext和1个ImageButton来创建运行时ROW,我使用PNG图像,但是当我尝试创建透明背景时,结果是或者灰色图像或白色图像(参见黄色箭头)......

何时

这是添加ROW的运行时代码

ImgBtAdd.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {

                LayoutInflater layoutInflater = (LayoutInflater) appContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                final View addView = layoutInflater.inflate(R.layout.rrow_ing, null);

                AutoCompleteTextView et_ing = (AutoCompleteTextView) addView.findViewById(R.id.et_ingrediente);
                EditText et_qty = (EditText) addView.findViewById(R.id.et_qty);
                et_ing.setAdapter(adapter);
                //textOut.setText(et_ing.getText().toString());
                final ImageButton ImgBtClear = (ImageButton ) addView.findViewById(R.id.imgBt_clear);

                //ImgBtClear.setBackgroundColor(Color.TRANSPARENT); //don't work
                //ImgBtClear.setBackgroundResource(0);              //don't work

                ImgBtClear.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ((LinearLayout)addView.getParent()).removeView(addView);
                        listAllAddView();
                    }
                });

                //ImgBtClear.setOnClickListener(thisListener);
                IngContainer.addView(addView);
                listAllAddView();
            }
        });

这是ROW的XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

        <android.support.design.widget.TextInputLayout
            android:id="@+id/input_name"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:layout_toLeftOf="@+id/input_qty"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true">

            <AutoCompleteTextView
                android:id="@+id/et_ingrediente"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/txt_ingrediene"
                android:text=""
                android:inputType="text"
                app:errorEnabled="true"/>
        </android.support.design.widget.TextInputLayout>

    <android.support.design.widget.TextInputLayout
        android:id="@+id/input_qty"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toLeftOf="@+id/imgBt_clear"
        android:layout_toStartOf="@+id/imgBt_clear"
        >
        <android.support.design.widget.TextInputEditText
            android:id="@+id/et_qty"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/txt_quantita"
            android:text=""
            android:inputType="number|numberDecimal"
            android:layout_alignParentTop="true"
            android:layout_toRightOf="@+id/input_qty"
            android:layout_toEndOf="@+id/input_qty"/>
    </android.support.design.widget.TextInputLayout>

    <ImageButton
        android:id="@+id/imgBt_clear"
        android:layout_width="60dp"
        android:layout_height="wrap_content"
        app:srcCompat="@drawable/directory_up"
        android:scaleType="centerInside"
        tools:ignore="ContentDescription"
        android:focusable="false"
        android:layout_alignParentRight="true"
        android:baselineAlignBottom="true"
        android:layout_alignBottom="@+id/input_qty"

        android:background="#00000000" //don't work
        android:background="@null" //don't work
        android:background="@android:color/transparent" //don't work

        android:layout_alignParentTop="true"/>
</RelativeLayout>

我尝试了不同的方法:

android:background="#00000000" //don't work
android:background="@null" //don't work
android:background="@android:color/transparent" //don't work

enter image description here

如果我使用 android:background ,这就是结果 如果我提取代码,结果是: enter image description here

我将此代码用于ADD按钮并且工作正常 机器人:背景=&#34;#00000000&#34;

我用这个参数编译:

compileSdkVersion 25
    buildToolsVersion "23.0.3"
    defaultConfig {
        applicationId "com.bandweb.myviewrecipes"
        minSdkVersion 16
        targetSdkVersion 23

1 个答案:

答案 0 :(得分:1)

在您的ImageView中,您必须使用android:src而不是app:srcCompat,所以:

改变这个:

app:srcCompat="@drawable/directory_up"

有了这个:

android:src="@drawable/directory_up"