Android dialog自定义样式
有些时候默认的样式不能满足需求,这个时候我们可以自己写xml来布局我们的dialog代码如下,
package com.example.testdialogdemo; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; public class TestDialogDemoActivity extends Activity { private Button btn; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); btn = (Button) findViewById(R.id.btn); btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { CreateDialog(); } }); } private void CreateDialog() { AlertDialog.Builder builder = new AlertDialog.Builder(this); AlertDialog dialog = builder.create(); dialog.show(); dialog.setContentView(R.layout.update_alert_dialog_force); TextView update_alert_title = (TextView) dialog.findViewById(R.id.update_alert_title); update_alert_title.setText("升级提醒"); TextView update_alert_content = (TextView) dialog.findViewById(R.id.update_alert_content); update_alert_content.setText("软件升级\n软件升级\n软件升级\n软件升级\n"); Button update_alert_dialog_btn_ok = (Button) dialog.findViewById(R.id.update_alert_dialog_btn_ok); update_alert_dialog_btn_ok.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Toast.makeText(getApplicationContext(), "shengji ", 1).show(); } }); } }
xml如下,
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="210dp" android:background="@android:color/black" android:orientation="vertical" > <RelativeLayout android:layout_width="250dp" android:layout_height="wrap_content" android:gravity="center_vertical" android:paddingBottom="2dp" android:paddingLeft="5dp" android:paddingRight="5dp" android:paddingTop="2dp" > <ImageView android:id="@+id/update_alert_ico" android:layout_width="30dp" android:layout_height="30dp" android:src="@drawable/dyd" /> <TextView android:id="@+id/update_alert_title" android:layout_width="wrap_content" android:layout_height="30dp" android:layout_marginLeft="2dp" android:gravity="center_vertical" android:layout_toRightOf="@+id/update_alert_ico" android:textSize="14.0sp" android:textStyle="bold" /> </RelativeLayout> <View android:layout_width="wrap_content" android:layout_height="2dp" android:background="#666666" /> <ScrollView android:layout_width="fill_parent" android:layout_height="100dp" android:layout_margin="10dp" android:scrollbars="none" > <TextView android:id="@+id/update_alert_content" android:layout_width="fill_parent" android:layout_height="fill_parent" android:textColor="@android:color/white" android:textSize="14.0sp" > </TextView> </ScrollView> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" > <Button android:id="@+id/update_alert_dialog_btn_ok" android:layout_width="120dip" android:layout_height="wrap_content" android:layout_marginBottom="5dp" android:layout_marginTop="5dp" android:background="@drawable/btn_bg_alertdialog_def_left" android:gravity="center" android:text="立即升级" android:textColor="@android:color/black" android:textSize="14.0sp" android:textStyle="bold" > </Button> </LinearLayout> </LinearLayout>