【发布时间】:2015-02-18 09:25:27
【问题描述】:
我的目标是 API 级别 8+。我需要一个带有标题、消息、带有一些文本的复选框和两个经典底部按钮的对话框。 我正在尝试使用标准的 AlertDialog 和方法 setView 来添加复选框:
checkbox.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/frame_checkbox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp" >
<CheckBox
android:id="@+id/checkbox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:checked="true"
android:textSize="14sp" >
</CheckBox>
</FrameLayout>
showDialog 方法
private void showDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setTitle("Title");
builder.setMessage("Message");
View checkboxView = View.inflate(activity, R.layout.checkbox, null);
CheckBox checkBox = (CheckBox) checkboxView.findViewById(R.id.checkbox);
checkBox.setText("Some checkbox text");
builder.setView(checkboxView);
// First button
builder.setNegativeButton(getString(R.string.not_now),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
});
// Second button
builder.setPositiveButton(getString(R.string.help),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
});
builder.create();
builder.show();
}
我的 AppBaseTheme 有父级“Theme.AppCompat.Light”(/res/values/styles.xml),对于 values-v11 和“Theme.AppCompat.Light.DarkActionBar”对于 values-v14 相同。 我在 2.3.3 中遇到问题,复选框和文本没有边距或填充(我已在 xml 中设置它们),除了文本是黑色作为背景。在 4.4 中一切看起来都很好。 我已经阅读了很多关于这个问题的东西,但我仍然感到困惑并且没有解决方案。你能帮帮我吗?
【问题讨论】:
-
你可以在这里看到我对同一问题的回答stackoverflow.com/a/33377490/3346625
标签: android android-layout android-dialog android-dialogfragment android-styles