【发布时间】:2017-01-09 16:27:50
【问题描述】:
我的自定义 AlertDialog 出现问题。 如您所见here,当我使用权重机制时,缺少正/负按钮文本的位并且视图相互重叠。我在模拟器上测试:Nexus 5x Google API's level 22。我有两个问题:
- 如何修复文本中的缺失位?
- 如何让视图不重叠?
这是我的自定义 AlertDialog 布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/sighting_dialog"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">
<TextView
android:id="@+id/sighting_dialog_title"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.2"
android:textAppearance="?android:attr/textAppearanceLarge"/>
<TextView
android:id="@+id/sighting_dialog_team_label"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.1"/>
<ImageView
android:id="@+id/sighting_dialog_snapshot"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.5"
android:adjustViewBounds="true"
android:layout_margin="5dp"/>
<EditText
android:id="@+id/sighting_dialog_info_edit"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.2"
android:hint="@string/sighting_input_dialog_hint">
</EditText>
</LinearLayout>
这里我构建了AlertDialog,缓冲区实现了OnClickListener接口。
/**
* Sets the Context for the Dialog of the SightingSession.
* */
public Builder setDialogContext(Context context) {
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.sighting_input_dialog, null);
buffer.dialog = new AlertDialog.Builder(context)
.setCancelable(false)
.setPositiveButton("Bevestigen", buffer)
.setNegativeButton("Annuleren", buffer)
.setView(view)
.create();
return this;
}
在这段代码中,我显示并使用了对话框。第一个 OnClick 方法用于 AlertDialog,第二个用于 Snackbar。在 onSnapshotReady() 方法中,我设置了 ImageView 的可绘制对象。
@Override
public void onClick(DialogInterface dialogInterface, int i) {
switch (i)
{
case AlertDialog.BUTTON_POSITIVE:
if(callback != null) {
callback.onSightingCompleted(lastLatLng, deelgebied, ((TextView)dialog.findViewById(R.id.sighting_dialog_info_edit)).getText().toString());
destroy();
}
break;
case AlertDialog.BUTTON_NEGATIVE:
snackbar.show();
break;
}
}
@Override
public void onClick(View view) {
if(deelgebied != null) {
googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(lastLatLng, 12), new GoogleMap.CancelableCallback() {
@Override
public void onFinish() {
if(dialog != null) {
dialog.show();
((TextView)dialog.findViewById(R.id.sighting_dialog_title)).setText("Bevestig de " + type);
((TextView)dialog.findViewById(R.id.sighting_dialog_team_label)).setText("Deelgebied: " + deelgebied.getName());
}
googleMap.snapshot(SightingSession.this);
}
@Override
public void onCancel() {
}
});
} else {
snackbar.setText("Selecteer een geldige locatie!");
snackbar.show();
}
}
@Override
public void onSnapshotReady(Bitmap bitmap) {
if(dialog != null) {
((ImageView)dialog.findViewById(R.id.sighting_dialog_snapshot)).setImageDrawable(new BitmapDrawable(Japp.getAppResources(), bitmap));
}
}
按照 Pztar 的要求,这里是 styles.xml 文件。
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:windowBackground">@drawable/splash_screen</item>
</style>
<style name="ShowCaseTheme" parent="ShowcaseView">
<item name="sv_backgroundColor">#e51249d9</item>
<item name="sv_showcaseColor">#9966ff</item>
<item name="sv_buttonText">Oke</item>
<item name="sv_titleTextAppearance">@style/ShowCaseTitle</item>
<item name="sv_detailTextAppearance">@style/ShowCaseDetail</item>
</style>
<style name="ShowCaseTitle" parent="TextAppearance.ShowcaseView.Title">
<item name="android:textColor">@color/colorAccent</item>
</style>
<style name="ShowCaseDetail" parent="TextAppearance.ShowcaseView.Title.Light">
<item name="android:textColor">#deffffff</item>
</style>
<style name="AlertDialog" parent="Theme.AppCompat.Light.Dialog.Alert"></style>
</resources>
【问题讨论】:
-
尝试在
AlertDialog.Builder构造函数中提供theme,例如。new AlertDialog.Builder(context, R.style.Theme_AppCompat_Dialog_Alert) -
@Pztar 我用父级
AlertDialog.AppCompat.Light创建了一个自定义主题,并设置了AlertDialog 的主题。这似乎解决了丢失的位和重叠的视图,但现在 AlertDialog 占据了整个屏幕。我无法点击正/负按钮,我可以让 AlertDialog 再次恢复正常大小吗? -
确保你是从
Alert扩展而来的,而不是一般的父主题。 -
我无法编辑我的评论,但你的
style看起来像这样,<style name="MyCustomDialog" parent="Theme.AppCompat.Light.Dialog.Alert"> -
@Pztar 我现在用这个样式
<style name="AlertDialog" parent="Theme.AppCompat.Light.Dialog.Alert"></style>现在对话框不再全屏了,但是其他两个问题又来了
标签: android android-alertdialog android-layout-weight