ポップアップウィンドウシリーズ:ダイアログカスタムレイアウトUIの例

  • 数年前のAndroid開発インターンとして、私が最初に開発した機能はチェックインポップアップウィンドウだったことを今でも覚えています。現在、DialogFragmentはポップアップウィンドウで使用されていますが、Dialogを直接書き直せないのはなぜですか?自分で練習してみたところ、Dialogを使うのは面倒ではないことがわかりました。
  • デザインドラフトとデモの効果を見ると、デモは時間の一部しか実現していませんでした。
  • メインピット:

    • ダイアログにはタイトルバーが付属していますが、削除する必要があります。

    • Dialogには、角が丸いポップアップウィンドウが付属しており、組み込みの背景の上にカスタムの丸い背景が浮かんでいます。また、クリアする必要があります。

メインコード:

  • MainActivity
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        BaseDialog dialog = new BaseDialog(MainActivity.this,R.layout.my_dialog);
        dialog.show();
    }
}
  • BaseDialog
public class BaseDialog extends Dialog {
    View view;
    public BaseDialog(Context context, int layoutId) {
        super(context);
        view = View.inflate(context, layoutId, null);
    }



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        getWindow().requestFeature(Window.FEATURE_NO_TITLE);//去除标题
        super.onCreate(savedInstanceState);
        if (view != null) {
            setContentView(view);
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        getWindow().setBackgroundDrawableResource(android.R.color.transparent); //去除自带的背景
    }
}

ポップアップウィンドウレイアウトファイル:my_dialog.xml

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

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

        <RelativeLayout
            android:id="@+id/rl"
            android:layout_width="313dp"
            android:layout_height="213dp"
            android:layout_centerHorizontal="true"
            android:background="@drawable/dialog_bg">

        </RelativeLayout>

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/rl"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="41dp"
            android:src="@drawable/close" />
    </RelativeLayout>


</RelativeLayout>

角の丸い背景ファイル:ドローアブルフォルダー内のdialog_bg.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:radius="15dp"/>
    <solid android:color="#FFFFFF"/>
</shape>

次のブロガーのおかげで:
Androidで丸みを帯びた角カットする一般的な方法
は、丸みを帯びたダイアログの黒(白)の背景の問題を解決します

おすすめ

転載: blog.csdn.net/zhangjin1120/article/details/114492542