文本的AlertDialog位丢失且未遵循权重分布

时间:2016-09-01 17:17:01

标签: android alertdialog android-layout-weight

我的自定义AlertDialog出现问题。 正如您所看到的here,当我使用weight-mechanisme时,正/负按钮文本的位缺失并且视图相互重叠。我在模拟器上测试:Nexus 5x Google API的第22级。我有两个问题:

  1. 如何修复文本中缺少的位?
  2. 如何让视图不相互重叠?
  3. 这是我的自定义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的drawable。

    @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>
    

1 个答案:

答案 0 :(得分:0)

我更改了自定义对话框布局,现在顶级视图是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>