【问题标题】:Android AlertDialog Not Displaying Entire XMLAndroid AlertDialog 不显示整个 XML
【发布时间】:2017-06-04 20:32:52
【问题描述】:

有点奇怪的问题,我有一个布局,在我的 Android 应用程序中作为 AlertDialog 打开,但由于某种原因,只显示了 xml 文件中两个 LinearLayouts 中的第二个。我没有意识到布局可能会部分忽略其视图。奇怪的是,如果我使用 PopupWindow 而不是 AlertDialog,布局会正确绘制,所以我认为导致问题的两者之间存在一些差异。

有问题的 XML 布局如下。从根本上讲,它是一个带有两个孩子的 LinearLayout,其权重分别为 0.10 和 0.90。绘制时,权重为 0.90 的布局会填满整个父级,而权重为 0.10 的布局无处可寻。更改这些权重值并不能解决此问题。

解决方法是什么?

menu_popup_set_chords.xml

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_height="match_parent"
android:layout_width="match_parent">

<!-- This is the missing Layout on draw -->
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:orientation="vertical"
    android:layout_weight="0.10"
    android:background="#000000">
</LinearLayout>

<!-- This layout fills the entire dialog window instead of 90% of it -->
<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:background="#212121"
    android:layout_weight="0.90">

    <LinearLayout
        android:layout_weight="1.40"
        android:layout_width="0dp"
        android:layout_height="match_parent">

        <!-- Custom view for Canvas -->
        <com.example.foo.CustomChordCanvas
            android:id="@+id/chordCanvas"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/neck_hud_background_alpha85"/>

    </LinearLayout>

    <LinearLayout
        android:orientation="vertical"
        android:padding="8dp"
        android:layout_weight="3.60"
        android:layout_width="0dp"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="3.10">

            <android.support.v7.widget.RecyclerView
                android:id="@+id/rv_userChords"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="0.5">

            <LinearLayout
                android:id="@+id/fl_actionButtons"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="center"
                android:orientation="horizontal">

                <LinearLayout
                    android:layout_weight="0.5"
                    android:layout_width="0dp"
                    android:layout_height="match_parent">

                    <android.support.v7.widget.AppCompatImageButton
                        android:id="@+id/addMarker"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_gravity="center"
                        android:padding="16dp"
                        android:clickable="true"
                        android:src="@drawable/ic_add_circle_black_24dp"
                        android:background="@android:color/transparent"
                        android:tint="#C4252C"/>

                </LinearLayout>

                <LinearLayout
                    android:layout_weight="0.5"
                    android:layout_width="0dp"
                    android:layout_height="match_parent">

                    <android.support.v7.widget.AppCompatImageButton
                        android:id="@+id/removeMarker"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_gravity="center"
                        android:background="@android:color/transparent"
                        android:padding="16dp"
                        android:src="@drawable/ic_remove_circle_black_24dp"
                        android:tint="#C4252C"/>

                </LinearLayout>

            </LinearLayout>


        </LinearLayout>

    </LinearLayout>

</LinearLayout>

为了全面起见,以下是构建和显示相关 AlertDialog 的代码:

FragmentChordMenu.java

public class FragmentChordMenu extends Fragment implements CustomChordAdapter.onItemClickListener {
public static int chordCanvasWidth, chordCanvasHeight;

private static RecyclerView mCustomChordList;
private static CustomChordAdapter mRecyclerViewAdapter;
private static Context mContext;

private FloatingActionButton mFAB;
private View mPopupView;
private AlertDialog mDialogChordMenu;
private CustomChordCanvas chordCanvas;
private ImageButton btnAddMarker, btnRemoveMarker;

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    mRecyclerViewAdapter = new CustomChordAdapter(this);
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
    mContext = getActivity();   //stores application context for later use in fragment without risk
                                                        //of detachment

    View v = inflater.inflate(R.layout.menu_fragment_chord, container, false);
    LayoutInflater layoutInflater = (LayoutInflater)getActivity().getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    mPopupView = layoutInflater.inflate(R.layout.menu_popup_set_chords, null);

    mDialogChordMenu = new AlertDialog.Builder(getActivity()).setView(mPopupView).create();

    chordCanvas = (CustomChordCanvas) mPopupView.findViewById(R.id.chordCanvas);

    ...

    //This method fetches view parameters for one of the Dialog child layout 
    //A ViewTreeObserver is used to allow view parameters to be accessed BEFORE view is drawn on screen
    ViewTreeObserver viewTreeObserver = chordCanvas.getViewTreeObserver();
    if (viewTreeObserver.isAlive()) {
        viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                chordCanvas.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                chordCanvasWidth = chordCanvas.getMeasuredWidth();
                chordCanvasHeight = chordCanvas.getMeasuredHeight();
                CustomChordCanvas.setViewParams(chordCanvasWidth, chordCanvasHeight);
            }
        });
    }

    mFAB = (FloatingActionButton) v.findViewById(R.id.addChord);
    mFAB.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            mDialogChordMenu.show();
            mCustomChordList = (RecyclerView) mPopupView.findViewById(R.id.rv_userChords);
            LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
            mCustomChordList.setLayoutManager(layoutManager);
            mCustomChordList.setAdapter(mRecyclerViewAdapter);
        }
    });

    return v;
}
...}

【问题讨论】:

  • 尝试为对话框自定义布局提供固定高度宽度。

标签: android xml android-layout dialog android-alertdialog


【解决方案1】:

为对话框的自定义布局设置一些默认高度。

【讨论】:

  • 对高度进行编码确实会使缺少的线性布局出现!所以现在的问题是,如何让我的原始布局权重适用于有问题的两种布局(0.10 和 0.90)?
猜你喜欢
  • 2021-12-17
  • 1970-01-01
  • 1970-01-01
  • 2013-01-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多