【问题标题】:Set RelativeLayout background to full transparent in Android Dialog在Android Dialog中将RelativeLayout背景设置为完全透明
【发布时间】:2018-12-27 11:34:07
【问题描述】:

我已经为弹出对话框创建了一个 xml 布局。在那里,我创建了一个 CardView,其顶部有一个图像。

一切正常,但图像背景的一半应该是完全透明的,这意味着不应该看到任何视图。我尝试设置 alpha 值,但它没有按预期工作。它隐藏了所有视图。我也试过@android:color:transparent。

这是我的代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

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

    <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"

        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="40dp"
        android:clickable="true"
        android:focusable="true"
        android:foreground="?android:attr/selectableItemBackground"
        app:cardBackgroundColor="@android:color/transparent"
        app:cardCornerRadius="10dp"
        app:cardPreventCornerOverlap="false"
        app:cardUseCompatPadding="true">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">


            <LinearLayout
                android:id="@+id/linear_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_marginTop="5dp"
                android:layout_marginRight="30dp"
                android:orientation="vertical">


                <TextView
                    android:id="@+id/textview1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Total Project"
                    android:textStyle="bold" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:text="100" />

            </LinearLayout>

            <LinearLayout
                android:id="@+id/linearbox"
                android:layout_width="match_parent"
                android:layout_height="100dp"
                android:layout_below="@+id/linear_text"
                android:layout_marginTop="25dp"
                android:weightSum="3">

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:orientation="vertical">

                    <View
                        android:layout_width="match_parent"
                        android:layout_height="1dp"
                        android:background="@color/colorGrayLight" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:padding="4dp"
                        android:text="Project Live"
                        android:textStyle="bold" />


                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:padding="4dp"
                        android:text="5"
                        android:textStyle="bold" />


                </LinearLayout>

                <View
                    android:layout_width="1dp"
                    android:layout_height="wrap_content"
                    android:background="@color/colorGrayLight" />

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:orientation="vertical">

                    <View
                        android:layout_width="match_parent"
                        android:layout_height="1dp"
                        android:background="@color/colorGrayLight" />

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:gravity="center"
                        android:padding="4dp"
                        android:text="Project Wip"
                        android:textStyle="bold" />


                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:gravity="center"
                        android:padding="4dp"
                        android:text="5"
                        android:textStyle="bold" />


                </LinearLayout>

                <View
                    android:layout_width="1dp"
                    android:layout_height="wrap_content"
                    android:background="@color/colorGrayLight" />

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:orientation="vertical">

                    <View
                        android:layout_width="match_parent"
                        android:layout_height="1dp"
                        android:background="@color/colorGrayLight" />

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:gravity="center"
                        android:padding="4dp"
                        android:text="Project Closed"
                        android:textStyle="bold" />


                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:gravity="center"
                        android:padding="4dp"
                        android:text="5"
                        android:textStyle="bold" />


                </LinearLayout>
            </LinearLayout>
        </RelativeLayout>
    </android.support.v7.widget.CardView>

</LinearLayout>


<ImageView
    android:layout_width="80dp"
    android:layout_height="80dp"
    android:layout_marginStart="10dp"
    android:layout_marginLeft="10dp"
    android:src="@drawable/admin"
    tools:ignore="ContentDescription" />

对话框:

  private void showDialog() {
    // creating the fullscreen dialog
    final Dialog dialog = new Dialog(this);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setContentView(R.layout.dialog);

    if (dialog.getWindow() != null) {
        //  dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.WHITE));
        dialog.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;
        dialog.getWindow().setLayout(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
    }
    dialog.setCanceledOnTouchOutside(false);
    dialog.setCancelable(true);
    dialog.show();
    }

【问题讨论】:

    标签: android android-layout android-relativelayout


    【解决方案1】:

    如下所示更改您的 imageview 代码。

    <ImageView
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginStart="10dp"
        android:layout_marginLeft="10dp"
        android:foregroundGravity="center"
        android:src="@drawable/ic_action_next"
        tools:ignore="ContentDescription" 
        android:translationZ="10dp"/>
    

    我在此代码中的更改添加了参数 android:layout_centerHorizo​​ntal="true" 这将使图像视图居中并添加 android:translationZ="10dp" 这将始终将 imageview 保持在其他布局之上。

    希望对你有帮助

    【讨论】:

      【解决方案2】:

      您可以将 alpha 添加到背景颜色以使图像透明。

      public static int getColorWithAlpha(int color, int alpha) {
          int red = Color.red(color);
          int green = Color.green(color);
          int blue = Color.blue(color);
          return Color.argb(alpha, red, green, blue);
      }
      

      【讨论】:

        猜你喜欢
        • 2017-02-02
        • 1970-01-01
        • 1970-01-01
        • 2013-07-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多