【轉】Android AlertDialog自訂布局

來源:互聯網
上載者:User

標籤:main   tools   content   inflate   之間   size   extend   mat   xmlns   

原文網址:53022294

由於開發中經常使用彈框,然而系統內建的彈框太局限,也不太美觀,經常不能滿足開發需求,所以就只能自訂布局。其實自訂布局很簡單,沒不要寫出來,但是如果不寫一遍的,後面遇到的話就感覺又會忘記,所以在次記一小筆,僅記一個最簡單的例子,可以舉一反三。 

直接上代碼

public class MainActivity extends Activity implements OnClickListener {    private TextView text1, text2;    private Context mContext;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        requestWindowFeature(Window.FEATURE_NO_TITLE);        setContentView(R.layout.activity_main);        mContext = this;        initView();    }    private void initView() {        text1 = (TextView) findViewById(R.id.text1);        text2 = (TextView) findViewById(R.id.text2);        text1.setOnClickListener(this);        text2.setOnClickListener(this);    }    @Override    public void onClick(View v) {        switch (v.getId()) {        case R.id.text1:            dialogShow1();            break;        case R.id.text2:            dialogShow2();            break;        default:            break;        }    }    private void dialogShow1() {        AlertDialog.Builder builder = new Builder(mContext);        builder.setTitle("溫馨提示");        builder.setIcon(R.drawable.ic_launcher);        builder.setMessage("原理是基本");        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {            @Override            public void onClick(DialogInterface arg0, int arg1) {                Toast.makeText(mContext, "no", 1).show();            }        });        builder.setPositiveButton("立即更新",                new DialogInterface.OnClickListener() {                    @Override                    public void onClick(DialogInterface arg0, int arg1) {                        Toast.makeText(mContext, "ok", 1).show();                    }                });        Dialog dialog = builder.create();        dialog.show();    }    /**     * 自訂布局     * setView()只會覆蓋AlertDialog的Title與Button之間的那部分,而setContentView()則會覆蓋全部,     * setContentView()必須放在show()的後面     */    private void dialogShow2() {        AlertDialog.Builder builder = new Builder(mContext);        LayoutInflater inflater = LayoutInflater.from(mContext);        View v = inflater.inflate(R.layout.update_manage_dialog, null);        TextView content = (TextView) v.findViewById(R.id.dialog_content);        Button btn_sure = (Button) v.findViewById(R.id.dialog_btn_sure);        Button btn_cancel = (Button) v.findViewById(R.id.dialog_btn_cancel);        //builer.setView(v);//這裡如果使用builer.setView(v),自訂布局只會覆蓋title和button之間的那部分        final Dialog dialog = builder.create();        dialog.show();        dialog.getWindow().setContentView(v);//自訂布局應該在這裡添加,要在dialog.show()的後面        //dialog.getWindow().setGravity(Gravity.CENTER);//可以設定顯示的位置        btn_sure.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View v) {                dialog.dismiss();                Toast.makeText(mContext, "ok", 1).show();            }        });        btn_cancel.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View arg0) {                dialog.dismiss();                Toast.makeText(mContext, "no", 1).show();            }        });    }}

activity_main的布局

<LinearLayout 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"    android:layout_marginTop="100dp"    android:orientation="vertical" >    <TextView        android:id="@+id/text1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_margin="10dp"        android:gravity="center"        android:text="彈出dialog"        android:textSize="@dimen/activity_horizontal_margin" />    <TextView        android:id="@+id/text2"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_margin="10dp"        android:gravity="center"        android:text="彈出自訂布局dialog"        android:textSize="@dimen/activity_horizontal_margin" /></LinearLayout>

update_manage_dialog布局

<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"    android:background="#00FFFFFF" >    <RelativeLayout        android:layout_width="250dp"        android:layout_height="250dp"        android:layout_centerInParent="true"        android:background="@drawable/update_bg" >        <TextView            android:id="@+id/dialog_title"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_marginTop="30dp"            android:gravity="center"            android:text="溫馨提示"            android:textSize="18sp" />        <TextView            android:id="@+id/dialog_content"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_below="@+id/dialog_title"            android:layout_marginTop="10dp"            android:layout_marginLeft="30dp"            android:layout_marginRight="30dp"            android:text="原理是基本\n實踐出真知"            android:textSize="14sp" />        <LinearLayout            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_alignParentBottom="true"            android:orientation="horizontal" >            <Button                android:id="@+id/dialog_btn_cancel"                android:layout_width="0dp"                android:layout_height="wrap_content"                android:layout_weight="1"                android:background="@null"                android:text="取消"                android:textColor="#AAAAAA"                android:textSize="14sp" />            <Button                android:id="@+id/dialog_btn_sure"                android:layout_width="0dp"                android:layout_height="wrap_content"                android:layout_weight="1"                android:background="@null"                android:text="立即更新"                android:textSize="14sp" />        </LinearLayout>    </RelativeLayout></RelativeLayout>

update_bg放在drawable裡面,代碼如下

<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android" >    <!-- android:radius 弧形的半徑 -->    <corners android:radius="30dp" />    <!-- 填充的顏色 -->    <solid android:color="@android:color/white" /></shape>

【轉】Android AlertDialog自訂布局

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.