我正在尝试使用新的AppCompat 22.1
功能AppCompatDialog
,但它会显示我没有标题的对话框,但我使用方法setTitle
。如果我将AppCompatDialog
更改为Dialog
,一切正常。它是AppCompatDialog
中的错误?
这是我的对话框代码:
final AppCompatDialog dialog = new AppCompatDialog(ctx);
dialog.setTitle(R.string.choose_mode);
dialog.setContentView(R.layout.dialog_with_list);
ListView listView = (ListView) dialog.findViewById(R.id.list_view);
List<ModeItemModel> modesList = new ArrayList<ModeItemModel>();
modesList.add(new ModeItemModel(GUI_MANUAL));
modesList.add(new ModeItemModel(GUI_GPS));
listView.setAdapter(new ModeItemsAdapter(ctx, R.layout.list_item_ico_with_text, modesList));
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
//Do Action
dialog.cancel();
}
});
dialog.setCancelable(true);
dialog.show();
答案 0 :(得分:6)
正如@Eddwhis在上面的评论中所指出的,主题中的<item name="windowNoTitle">true</item>
也隐藏了对话框的标题。
我通过添加自定义AppCompatDialgStyle来修复它,我在其中将<item name="windowNoTitle">false</item>
设置为false。
基本主题:
<style name="Theme.App.Base" parent="Theme.AppCompat.Light.DarkActionBar,">
...
<item name="alertDialogTheme">@style/Theme.App.AppCompatDialogStyle</item>
<item name="dialogTheme">@style/Theme.App.AppCompatDialogStyle</item>
</style>
NoActionBar主题:
<style name="Theme.App.NoActionBar" parent="@style/Theme.App.Base">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
我的对话样式:
<style name="Theme.App.AppCompatDialogStyle" parent="Theme.AppCompat.Light.Dialog">
<item name="colorAccent">#f00</item>
<item name="android:textColorPrimary">@color/black</item>
<item name="android:background">@color/red</item>
<item name="windowNoTitle">false</item> <!-- that's the important bit -->
</style>
答案 1 :(得分:0)
这段代码对我有用
活动
public class MyActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
final AppCompatDialog dialog = new AppCompatDialog(this);
dialog.setTitle("Hello For Dialog");
dialog.setContentView(R.layout.dialog_nm);
// ListView listView = (ListView) dialog.findViewById(R.id.list_view);
//
// List<ModeItemModel> modesList = new ArrayList<ModeItemModel>();
// modesList.add(new ModeItemModel(GUI_MANUAL));
// modesList.add(new ModeItemModel(GUI_GPS));
// listView.setAdapter(new ModeItemsAdapter(ctx, R.layout.list_item_ico_with_text, modesList));
// listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//
// @Override
// public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
// //Do Action
//
// dialog.cancel();
// }
// });
dialog.setCancelable(true);
dialog.show();
}
}
activity_my.xml
<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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".My">
<TextView
android:text="@string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
对话框布局xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:gravity="center"
android:background="@android:color/white"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Your message"
android:id="@+id/textView" android:layout_gravity="center_horizontal"/>
</LinearLayout>
styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>
</resources>