使视图显示在其他视图之上并始终显示在屏幕上

时间:2020-03-15 10:54:57

标签: android android-layout

enter image description here

我有这个布局

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView
        android:id="@+id/imgDisplay"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:scaleType="centerCrop" />
    <LinearLayout
        android:id="@+id/MainLinearLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical"
        android:background="@drawable/bg"
        android:keepScreenOn="true"
        tools:context=".MainActivity">
        <TextView
            style="@style/MyTextStyle"
            android:id="@+id/MessageTextView"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:gravity="center"
            android:layout_marginTop="20dp"
            android:layout_marginBottom="20dp"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_centerHorizontal="true"
            android:visibility="gone"/>
            <ImageButton
                        android:src="@drawable/ic_angry"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/AngryImageButton"
                        android:layout_marginLeft="10dp"
                        android:layout_marginRight="10dp"
                        android:adjustViewBounds="true"
                        android:layout_gravity="center_horizontal"
                        android:background="@null"
                        android:scaleType="fitCenter" />

            <LinearLayout
            android:id="@+id/BottomLinearLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:gravity="bottom"
            android:layout_below="@+id/FacesHolderFrameLayout"
            android:orientation="horizontal"
            android:foregroundGravity="bottom">
            <Space
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1" />
            <ImageButton
                android:layout_weight="1"
                android:src="@drawable/ic_insert_chart_white_48dp"
                android:layout_width="wrap_content"
                android:layout_height="54dp"
                android:id="@+id/StatisticsImageButton"
                android:background="@null"
                android:layout_marginTop="20dp"
                android:scaleType="fitCenter" />
            <Space
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1" />
        </LinearLayout>
    </LinearLayout>
</RelativeLayout>

一般来说,这是一个将整个屏幕用作背景图像的imageview,在它上面,我有一个可垂直放置文本视图的linearlayout( MainLinearLayout ),一个imagebutton( AngryImageButton )和另一个线性布局( BottomLinearLayout ),该布局仅显示水平居中的小图像(如徽标)。

我希望能够以编程方式更改 AngryImageButton BottomLinearLayout 的大小,以显示在屏幕底部并始终可见。我的意思是站在其他视图之上。

现在 BottomLinearLayout 出现在中间,在 MainLinearLayout 下方,如果我将 AngryImageButton 变大,它将推到< strong> BottomLinearLayout ,直到它在屏幕上完全不可见为止,我不希望这样。我希望它在底部并始终可见。

我该如何做到?我也希望布局在两个方向上都能工作

1 个答案:

答案 0 :(得分:1)

要将View放在前面,可以使用

myView.bringToFront()

或您可以使用ViewCompat并按如下所示设置其翻译Z。

ViewCompat.setTranslationZ(view, 1)

这是bringToFront() bringToFront()

的文档
相关问题