如何以编程方式更改对话框的背景颜色?
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(false);
builder.setMessage(R.string.tnc)
//.setTitle(R.string.tnc_title)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Log.d(TAG, "OK Button Clicked");
dialog.dismiss();
finish();
}//end onClick
});
AlertDialog dialog = builder.create();
dialog.show();
答案 0 :(得分:1)
这是最好的方法!
首先以编程方式创建一个对话框。
public void onClick(View v) {
dialog1 = new Dialog(Page.this,R.style.myBackgroundStyle);
Window window = dialog1.getWindow();
window.setGravity(Gravity.CENTER);
window.setLayout(WindowManager.LayoutParams.FILL_PARENT,
WindowManager.LayoutParams.FILL_PARENT);
window.setBackgroundDrawable(new ColorDrawable());
dialog1.setTitle(null);
dialog1.requestWindowFeature(Window.FEATURE_NO_TITLE);
window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
dialog1.setContentView(R.layout.custom_dialog);
dialog1.getWindow().setBackgroundDrawable(
new ColorDrawable(android.graphics.Color.TRANSPARENT));
dialog1.setCanceledOnTouchOutside(true);
description.setText(DESCRIPTION);
heading_name.setText(PRODUCT_NAME);
Button abc = (Button)dialog1.findViewById(R.id.abc);
abc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
Button xyz = (Button) dialog1.findViewById(R.id.xyz);
retailer_btn.setOnClickListener(new View.OnClickListener() {
dialog1.dismiss();
}
});
dialog1.show();
}
});
现在创建一个新的xml文件并在xml(对话框布局)中设置此代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/description_layout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="45dp"
android:layout_gravity="top|right"
android:gravity="right|top"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/desription_layout2"
android:layout_width="160dp"
android:layout_height="90dp"
android:layout_marginRight="2dp"
android:padding="2dp"
android:background="@drawable/dropdown_white_background"
android:orientation="vertical" >
<Button
android:id="@+id/abc"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="2"
android:background="@drawable/curve_1"
android:text="abc"
android:textSize="18dp"
android:textColor="#000000"/>
<View
android:layout_width="fill_parent"
android:layout_height="2dp"
android:layout_marginBottom="1dp"
android:layout_marginTop="1dp"
android:background="#ffffff" />
<Button
android:id="@+id/xyz"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="2"
android:background="@drawable/curve_1"
android:text="xyz"
android:textSize="18dp"
android:textColor="#000000"/>
</LinearLayout>
</LinearLayout>
如果你想在你的对话框中做一些额外的事情,比如在对话框的对话框的四角进行四舍五入,那么在drawable中创建一个新的xml,并相应地添加它作为背景。
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape android:shape="rectangle">
<corners android:radius="3dip" />
<stroke android:width="1dip" android:color="#ffffff" />
<gradient android:angle="-90" android:endColor="#ccffff" android:startColor="#ccffff" />
</shape>
</item>
<item android:state_focused="true"><shape android:shape="rectangle">
<corners android:radius="3dip" />
<stroke android:width="1dip" android:color="#ffffff" />
<solid android:color="#66ffff" />
</shape>
</item>
<item>
<shape android:shape="rectangle">
<corners android:radius="3dip" />
<stroke android:width="1dip" android:color="#66ffff" />
<gradient android:angle="-90" android:endColor="#66ffff" android:startColor="#66ffff" />
</shape>
</item>
</selector>
希望这会有所帮助:)
答案 1 :(得分:0)
在这种情况下,您需要创建一个自定义对话框。
请参阅此教程,其中说明了这一点。 http://www.codeproject.com/Tips/659766/Android-Custom-DialogBox
跳起来有帮助。!!
答案 2 :(得分:0)
创建自定义布局并将其设置为对话框的布局。
您可以使用此代码:
final Dialog dialog = new Dialog(getActivity());
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.custom_dialog_box);