Android AlertDialog缺少文本的位且不遵循权重分布
我的自定义AlertDialog有问题。 正如您所看到的,当我使用权重机制时,正/负按钮文本缺失,视图相互重叠。我正在模拟器上测试:Nexus5xGoogleAPI的22级。我有两个问题:Android AlertDialog缺少文本的位且不遵循权重分布,android,android-alertdialog,android-layout-weight,Android,Android Alertdialog,Android Layout Weight,我的自定义AlertDialog有问题。 正如您所看到的,当我使用权重机制时,正/负按钮文本缺失,视图相互重叠。我正在模拟器上测试:Nexus5xGoogleAPI的22级。我有两个问题: 如何修复文本中缺少的位 如何才能让视图不相互重叠 这是我的自定义AlertDialog布局: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/
<?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>
在这段代码中,我显示并使用该对话框。第一个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>
@颜色/原色
@颜色/原色暗
@颜色/颜色重音
假的
真的
@可拉伸/飞溅屏幕
#e51249d9
#9966ff
奥克
@风格/陈列品名称
@风格/陈列柜细节
@颜色/颜色重音
#defffff
我更改了自定义对话框布局,现在顶层视图是滚动视图。这解决了我的问题,但不是解决办法。这是新的布局:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/sighting_dialog"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/sighting_dialog_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/sighting_dialog_team_label"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/sighting_dialog_snapshot"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:adjustViewBounds="true" />
<EditText
android:id="@+id/sighting_dialog_info_edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/sighting_input_dialog_hint">
</EditText>
</LinearLayout>
</ScrollView>
我更改了自定义对话框布局,现在顶层视图是滚动视图。这解决了我的问题,但不是解决办法。这是新的布局:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/sighting_dialog"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/sighting_dialog_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/sighting_dialog_team_label"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/sighting_dialog_snapshot"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:adjustViewBounds="true" />
<EditText
android:id="@+id/sighting_dialog_info_edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/sighting_input_dialog_hint">
</EditText>
</LinearLayout>
</ScrollView>
尝试在AlertDialog.Builder
构造函数中提供主题
,例如新建AlertDialog.Builder(上下文、R.style.theme\u AppCompat\u Dialog\u Alert)
@Pztar我创建了一个自定义主题,并设置了AlertDialog的主题。这似乎可以解决缺少的位和重叠视图,但现在AlertDialog占据了整个屏幕。我不能点击正/负按钮,我可以让AlertDialog再次变为正常大小吗?确保你是从Alert
扩展而不是一般的父主题。我不能编辑我的评论,但是你的样式会像这样,
@Pztar我现在使用这种样式
现在对话框不再全屏,但是其他两个问题又回来了,请尝试在AlertDialog.Builder
构造函数中提供主题,例如new-AlertDialog.Builder(context,R.style.theme\u-AppCompat\u-Dialog\u-Alert)
@Pztar我创建了一个自定义主题,并设置了AlertDialog的主题。这似乎可以解决缺少的位和重叠视图,但现在AlertDialog占据了整个屏幕。我不能点击正/负按钮,我可以让AlertDialog再次变为正常大小吗?确保你是从Alert
扩展而不是一般的父主题。我不能编辑我的评论,但是你的样式会像这样,
@Pztar我现在使用这种样式
现在对话框不再全屏,但另外两个问题又回来了