View不与其他View

时间:2015-08-06 12:55:13

标签: android android-layout android-view android-relativelayout android-viewgroup

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

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

需要在底部显示的视图,完全采用底部屏幕,但按钮&#34; BB&#34;出现 。

你能让我知道我在做什么错,所以按钮&#34; BB&#34;在底部添加新视图时应该隐藏吗?

请参阅此"BB" should hide with new view that i am adding

2 个答案:

答案 0 :(得分:1)

出于测试目的,请尝试使用(Relative, Linear etc.)Layout而不是BB Button,看看它是否适用于这种情况(我的意思是看红色布局是否位于其他布局的顶部) 。我怀疑这是因为Button指定了一个高程(默认情况下,不是你),而且它是#34;被提升的#34;在你正在展示的东西之上(我猜你在Lollipop上运行+)。如果是这种情况,您可以将红色布局的高程设置为大于Button的高程,或者使用已经具有该高程布局的布局(CardView?)

答案 1 :(得分:0)

Aah,花了一些时间(10分钟),但我明白了。你犯了两个错误,第一个是在膨胀,第二个是在获得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);

P.S。请在检查后标记为答案。