大家在開發的過程中,可能都碰過這樣的問題,系統提供的AlertDialog就是一個矩形的大白框,然後兩個按鈕,一個代表确定,一個代表取消。這樣再調用的過程中,整個界面顯得過于單調,如果應用裡面的界面大多都是鮮豔的顔色,這樣顯得更加醜陋。下面,我将針對這個問題進行詳細的講解。
最重要的工作:
要準備一個xml檔案,對話框裡面顯示這個界面;
我在這個裡面主要用到了selector+shape,這個界面就是用這兩個東西做出來的。如果這個東西用的不熟的可以直接看我上幾期的微網誌android中shape的使用和selector裡面使用shape;
先看一下效果圖吧
彈出對話框

點選按鈕時
這裡面和我們系統自帶的對話框表面上有什麼不同點呢?
1.整體布局是圓角矩陣,而系統自帶的是矩形;
2.裡面每個地方的顔色全部由自己來控制,系統的隻可以控制裡面的文字;
3.按鈕的位置和點選效果完全由自己控制。
怎麼實作的呢?
1.準備xml檔案
myalertdialog.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--對話框的有效區域-->
<RelativeLayout
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_gravity="center_vertical"
android:id="@+id/relativeLayout"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
<!--下面這個顔色尤為重要,否則你會發現四個邊角空為白色(因為這個大的布局裡面放圓角矩形,是以會空出來未填充的地方)-->
android:background="#646464" >
<!--上方天藍色的頭部(左上角,右上角為圓角)-->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_alignParentTop="true"
<!--shape中定義-->
android:background="@drawable/topdialog"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:gravity="center"
android:id="@+id/relativeLayout2">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="确定嗎?"
android:textColor="#ffffff"
android:textSize="30sp"
android:id="@+id/textView2"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
<!--下方天放置按鈕的底部(左下角,右下角為圓角)-->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/relativeLayout2"
android:layout_alignLeft="@+id/relativeLayout2"
android:layout_alignStart="@+id/relativeLayout2"
<!--shape中定義-->
android:background="@drawable/write_bg">
<!--确認按鈕-->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="确定"
android:textColor="#7A787D"
android:textSize="18sp"
android:id="@+id/button3"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="20dp"
android:background="@drawable/press_up"/>
<!--取消按鈕-->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="取消"
android:textColor="#7A787D"
android:textSize="18sp"
android:id="@+id/button4"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="20dp"
android:background="@drawable/press_up"/>
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>
頭部藍色
topdialog.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#00ffff"></solid>
<corners android:topLeftRadius="10dp"
android:topRightRadius="10dp"/>
<padding
android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="10dp"/>
</shape>
不太清楚可以看android中shape的使用
下面灰色區域
write_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#D6DADB"></solid>
<corners android:bottomLeftRadius="10dp"
android:bottomRightRadius="10dp"/>
<padding
android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="10dp"/>
</shape>
按鈕的控制
press_up.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!--未選中的狀态-->
<item android:state_selected="false">
<shape>
<solid android:color="#ACDCC4"></solid>
<corners android:radius="10dp" />
<padding android:bottom="10dp" android:left="10dp" android:right="10dp" android:top="10dp" />
</shape>
</item>
<!--選中的狀态-->
<item android:state_selected="true">
<shape>
<solid android:color="#89BD98"></solid>
<corners android:radius="10dp" />
<padding android:bottom="10dp" android:left="10dp" android:right="10dp" android:top="10dp" />
</shape>
</item>
</selector>
不太清楚可以看selector裡面使用shape;
android代碼:
public class MyAlertDialogActivity extends AppCompatActivity{
//點選這個按鈕顯示對話框
Button btn;
//對話框中左邊的按鈕
Button button3;
//對話框中右邊的按鈕
Button button4;
//藍色區域可以手動的進行設定哪些文本
TextView textView2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_alert_dialog);
textView2 = (TextView) findViewById(R.id.textView2);
btn = (Button) findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//建立一個AlertDialog對話框
AlertDialog alertDialog = new AlertDialog.Builder(MyAlertDialogActivity.this).create();
//顯示
alertDialog.show();
Window window = alertDialog.getWindow();
//對話框中顯示的哪一個布局
window.setContentView(R.layout.myalertdialog);
//從該布局中擷取相應的控件
button3 = (Button) window.findViewById(R.id.button3);
//點選事件
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MyAlertDialogActivity.this,"1",Toast.LENGTH_SHORT).show();
button3.setSelected(true);
button4.setSelected(false);
}
});
button4 = (Button) window.findViewById(R.id.button4);
button4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MyAlertDialogActivity.this,"2",Toast.LENGTH_SHORT).show();
button3.setSelected(false);
button4.setSelected(true);
}
});
}
});
}
}