【问题标题】:Android DialogFragment with custom layout remove white border [duplicate]具有自定义布局的Android DialogFragment删除白色边框[重复]
【发布时间】:2016-05-23 13:17:08
【问题描述】:

我有一个 DialogFragment 类,代码如下:

public class LeavingAppDialog extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

    LayoutInflater inflater = getActivity().getLayoutInflater();
    View v = inflater.inflate(R.layout.leaving_app_dialog, null);
    builder.setView(v);

    Typeface segoeuib = Typeface.createFromAsset(getActivity().getApplicationContext().getAssets(), "fonts/segoeuib.ttf");
    TextView text = (TextView) v.findViewById(R.id.msg_dialog);
    text.setTypeface(segoeuib);

    Dialog dialog = builder.create();



    return dialog;
    }
}

此对话框使用自定义布局,代码如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#00000000">

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true">

    <ImageView
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:id="@+id/imageView"
        android:src="@drawable/box_1_fit"
        android:scaleType="centerInside"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:background="#00000000" />

    <FrameLayout
        android:layout_width="275dp"
        android:layout_height="150dp"
        android:id="@+id/frameLayout"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="LEAVING THIS APP?"
            android:id="@+id/msg_dialog"
            android:textSize="35dp"
            android:layout_gravity="center_horizontal|top"
            android:gravity="center_horizontal|top"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp" />
    </FrameLayout>

    <Button
        android:layout_width="100dp"
        android:layout_height="37dp"
        android:id="@+id/btn_cancel"
        android:background="@drawable/cancel_button_fit"
        android:singleLine="false"
        android:layout_alignRight="@+id/frameLayout"
        android:layout_alignEnd="@+id/frameLayout"
        android:layout_marginTop="75dp"
        android:layout_below="@+id/frameLayout"
        android:layout_marginRight="25dp"
        android:onClick="ExitApp"/>

    <Button
        android:layout_width="100dp"
        android:layout_height="37dp"
        android:id="@+id/btn_accept"
            android:background="@drawable/accept_button_fit"
            android:layout_below="@+id/frameLayout"
            android:layout_alignLeft="@+id/frameLayout"
            android:layout_alignStart="@+id/frameLayout"
            android:layout_marginTop="75dp"
            android:layout_marginLeft="25dp"
            android:onClick="ExitApp"/>
    </RelativeLayout>
</RelativeLayout>

问题出在屏幕截图上。我需要摆脱这些白色边框。我试图通过布局编辑器和代码将背景颜色设置为透明。我尝试为所有布局和图像本身设置透明背景颜色,但这一切都没有效果。我的源图片是 png 文件,它确实具有透明背景,所以问题不在源图片上。

【问题讨论】:

标签: android layout background dialogfragment


【解决方案1】:

设置背景透明:

像这样:mDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));

【讨论】:

  • 这个方法我试过了,没用。在我尝试调用它的任何地方,它都会从 getWindow() 返回一个空引用。
【解决方案2】:

试试这个自定义警报对话框类:

class CustomAlertDialog extends AlertDialog {
    CustomAlertDialog(Context context) {
        super(context);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
        setContentView(R.layout.view_custom_alert_dialog);

        RMTextView txtTitle = (RMTextView) findViewById(R.id.txt_alert_title);

        txtTitle.setText("Title");
    }
}

【讨论】:

    【解决方案3】:

    试试这个方法。

    mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    // mDialog.getWindow().setBackgroundDrawableResource("#0069ad");
    mDialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
    

    【讨论】:

      猜你喜欢
      • 2018-06-16
      • 1970-01-01
      • 2015-05-27
      • 1970-01-01
      • 2016-02-10
      • 1970-01-01
      • 1970-01-01
      • 2018-05-30
      • 2015-02-05
      相关资源
      最近更新 更多