欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Dialog自定义灵活用法

程序员文章站 2024-03-22 11:16:40
...

记录一下dialog的灵活用法:
csdn下载地址:http://download.csdn.net/download/android_zhengyongbo/9944294
**

1.自定义dialog类

**

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.widget.TextView;

import com.example.administrator.myapplication.R;

public class CustomeDialog extends Dialog{
    private String message;
    private TextView tvMessage;
    /**
     * 参数个数是自己的需求而定
     */
    public CustomeDialog( Context context,String message) {
        super(context, R.style.new_setting_dialog);
        this.message=message;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.view_dialog);
        tvMessage= (TextView) findViewById(R.id.tv_message);
        tvMessage.setText(message);
    }
}

**

2.dialog布局文件(布局文件居中则dialog显示居中)

**

<?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"
    android:layout_width="match_parent"

    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="300dp"
        android:layout_height="200dp"
        android:layout_centerInParent="true"
        android:layout_gravity="center"
        android:background="#fff0"
        android:orientation="vertical">
        <TextView
            android:layout_marginTop="20dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="提示" />
        <TextView
            android:id="@+id/tv_message"
            android:layout_marginTop="50dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="this is text message" />
    </LinearLayout>

</RelativeLayout>

**

3.使用

**


import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import com.example.administrator.myapplication.R;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        CustomeDialog customeDialog=new CustomeDialog(this,"aaaaaa");
        customeDialog.show();
    }
}