视图不与其他视图重叠的问题

Issue with View not overlapping other View

我的要求是我需要显示一些需要在底部显示的事件的信息。我创建了一个布局,当该事件发生时,我使用以下方法:

XML布局

<RelativeLayout android:id="@+id/maincontainer"
            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:background="@android:color/white"
            android:paddingLeft="@dimen/activity_horizontal_margin"
            tools:context=".HomeActivity"
>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:text="BB"
    />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:text="AA"/>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginBottom="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="10dp"
    android:orientation="horizontal"
    android:weightSum="1">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_marginRight="5dp"
        android:layout_weight="0.5"
        android:text="PP"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_marginLeft="5dp"
        android:layout_weight="0.5"
        android:text="GG"/>
    </LinearLayout>
</RelativeLayout>

显示底视图的代码:

// To check main container where new view is to be added
       android.view.ViewGroup insertPoint = (android.view.ViewGroup) findViewById(R.id.maincontainer);

// New View that needs to be added
        android.view.LayoutInflater vi = (android.view.LayoutInflater) getApplicationContext().getSystemService(android.content.Context.LAYOUT_INFLATER_SERVICE);
        android.view.View v = vi.inflate(R.layout.app_flow_error, null);

        android.widget.TextView textView = (android.widget.TextView) v.findViewById(cR.id.information_message);
        textView.setText("Showing error");

//Defining the params value for new view that needs to be added

        android.widget.RelativeLayout.LayoutParams layoutParams = new android.widget.RelativeLayout.LayoutParams(android.view.ViewGroup.LayoutParams.MATCH_PARENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.addRule(android.widget.RelativeLayout.ALIGN_PARENT_BOTTOM);

        insertPoint.addView(v, insertPoint.getChildCount()-1, layoutParams);

        v.setHovered(true);

需要显示在底部的视图,完全占据底部屏幕,但按钮 "BB" 显示出来。

你能告诉我我到底做错了什么吗,所以当在底部添加新视图时按钮 "BB" 应该隐藏?

看到这个

出于测试目的,请尝试使用 (Relative, Linear etc.)Layout 而不是 BB Button 并查看它是否适用于这种情况(我的意思是查看红色布局是否在另一个布局之上布局)。我怀疑这是因为 Button 指定了一个高度(默认情况下,不是你指定的)并且它在你显示的内容之上 "raised" (我猜你是 Lollipop+ 上的 运行 ).如果是这种情况,您可以为红色布局设置一个高于 Button 的高度,或者使用已经具有该高度的布局 (CardView?)

啊,花了一些时间(10 分钟),但我弄明白了。你犯了两个错误,第一个错误是膨胀,第二个错误是获取布局参数,所以以下是对你有用的方法:

//test purpose
    android.view.ViewGroup insertPoint = (android.view.ViewGroup) view.findViewById(R.id.maincontainer);
    // New View that needs to be added
    android.view.LayoutInflater vi = (android.view.LayoutInflater)
            getApplicationContext().getSystemService(android.content.Context.LAYOUT_INFLATER_SERVICE);
    android.view.View v = vi.inflate(R.layout.app_flow_error, insertPoint, false);

    android.widget.TextView textView = (android.widget.TextView) v.findViewById(cR.id.information_message);
    textView.setText("Showing error");

    //Defining the params value for new view that needs to be added

    android.widget.RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) v.getLayoutParams();
    layoutParams.addRule(android.widget.RelativeLayout.ALIGN_PARENT_BOTTOM);
    v.setLayoutParams(layoutParams);
    insertPoint.addView(v,  layoutParams);

    v.setHovered(true);

P.S。请您核对后标记为答案