0

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

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”应该隐藏?

看到这个

4

2 回答 2

1

出于测试目的,请尝试使用 a(Relative, Linear etc.)Layout而不是 BBButton并查看它在这种情况下是否有效(我的意思是查看红色布局是否位于其他布局之上)。我怀疑这是因为Button指定了一个高度(默认情况下,不是您指定的),并且它在您显示的内容之上“升高”(我猜您正在运行 Lollipop+)。如果是这种情况,您可以为红色布局设置比Button's 更大的高度,或者使用已经具有该高度的布局(CardView?)

于 2015-08-06T14:22:26.527 回答
0

啊,花了一些时间(10 分钟),但我想通了。你犯了两个错误,第一个是inflating,第二个是获取layoutParams,所以这对你有用:

//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);

PS请在您检查后标记为答案。

于 2015-08-06T17:30:37.583 回答