簡體   English   中英

Android 在所有內容上疊加一個視圖?

[英]Android overlay a view ontop of everything?

你能在 android 中的所有東西上疊加一個視圖嗎?

在iPhone我會得到新的視圖設置其frame.origin到(0,0),其寬度和高度與寬度和高度self.view 將它添加到self.view會導致它作為一個覆蓋層,覆蓋后面的內容(或者如果它有一個透明的背景然后顯示后面的視圖)。

android中是否有類似的技術? 我意識到視圖略有不同(有三種類型(或更多......)相對布局、線性布局和框架布局)但是有什么方法可以不加選擇地將視圖覆蓋在所有內容之上?

只需使用RelativeLayoutFrameLayout 最后一個子視圖將覆蓋其他所有內容。

Android 支持 Cocoa Touch SDK 不支持的模式:布局管理。
iPhone布局意味着將所有內容絕對定位(除了一些拉伸因素)。 android 中的布局意味着孩子們將相對於彼此放置。

示例(第二個 EditText 將完全覆蓋第一個):

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/root_view">

    <EditText
        android:layout_width="fill_parent"
        android:id="@+id/editText1"
        android:layout_height="fill_parent">
    </EditText>

    <EditText
        android:layout_width="fill_parent"
        android:id="@+id/editText2"
        android:layout_height="fill_parent">
        <requestFocus></requestFocus>
    </EditText>

</FrameLayout>

FrameLayout是某種視圖堆棧。 專為特殊情況而設計。

RelativeLayout非常強大。 您可以定義規則,例如視圖 A 必須對齊父布局底部視圖 B 必須對齊 A 底部到頂部

根據評論更新

通常你在onCreate使用setContentView(R.layout.your_layout)設置內容(它會為你增加布局)。 您可以手動執行此操作並調用setContentView(inflatedView) ,沒有區別。

視圖本身可能是單個視圖(如TextView )或復雜的布局層次結構(嵌套布局,因為所有布局本身都是視圖)。

調用setContentView您的活動知道其內容是什么樣的,您可以使用(FrameLayout) findViewById(R.id.root_view)檢索此層次結構中的任何視圖(通用模式(ClassOfTheViewWithThisId) findViewById(R.id.declared_id_of_view) )。

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/root_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<LinearLayout
    android:id = "@+id/Everything"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <!-- other actual layout stuff here EVERYTHING HERE -->

   </LinearLayout>

<LinearLayout
    android:id="@+id/overlay"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="right" >
</LinearLayout>

現在,您在 LinearLayout 下使用android:id = "@+id/overlay"添加的任何視圖都將顯示為疊加, gravity = right在 LinearLayout with android:id="@+id/Everything"

您可以使用bringToFront

    View view=findViewById(R.id.btnStartGame);
    view.bringToFront();

最好的方法是ViewOverlay ,您可以將任何可繪制對象作為覆蓋添加到任何視圖作為其覆蓋自 Android JellyBeanMR2(Api 18) 以來。

mMyDrawable添加到mMyView作為其疊加層:

mMyDrawable.setBounds(0, 0, mMyView.getMeasuredWidth(), mMyView.getMeasuredHeight())
mMyView.getOverlay().add(mMyDrawable)

我剛剛為此做了一個解決方案。 我為此創建了一個,以可重用的方式執行此操作,這就是您無需在 XML 中重新編碼的原因。 這是有關如何在JavaKotlin 中使用它的文檔。 首先,從要顯示疊加層的活動中對其進行初始化-

AppWaterMarkBuilder.doConfigure()
                .setAppCompatActivity(MainActivity.this)
                .setWatermarkProperty(R.layout.layout_water_mark)
                .showWatermarkAfterConfig();

然后你可以在你的應用程序的任何地方隱藏和顯示它 -

  /* For hiding the watermark*/
  AppWaterMarkBuilder.hideWatermark() 

  /* For showing the watermark*/
  AppWaterMarkBuilder.showWatermark() 

Gif 預覽 -

預習

我之前嘗試過 awnsers,但這不起作用。 現在我使用了 LinearLayout 而不是 TextureView,現在它可以正常工作了。 希望它可以幫助其他有同樣問題的人。 :)

    view = (LinearLayout) findViewById(R.id.view); //this is initialized in the constructor
    openWindowOnButtonClick();

public void openWindowOnButtonClick()
{
    view.setAlpha((float)0.5);
    FloatingActionButton fb = (FloatingActionButton) findViewById(R.id.floatingActionButton);
    final InputMethodManager keyboard = (InputMethodManager) getSystemService(getBaseContext().INPUT_METHOD_SERVICE);
    fb.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            // check if the Overlay should be visible. If this value is false, it is not shown -> show it.
            if(view.getVisibility() == View.INVISIBLE)
            {
                view.setVisibility(View.VISIBLE);
                keyboard.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0);
                Log.d("Overlay", "Klick");
            }
            else if(view.getVisibility() == View.VISIBLE)
            {
                view.setVisibility(View.INVISIBLE);
                keyboard.toggleSoftInput(0, InputMethodManager.HIDE_IMPLICIT_ONLY);
            }

如上所述,behindToFront() 對於程序化調整非常容易。 由於 stateListAnimator,我在使用按鈕 z 順序時遇到了一些麻煩。 如果您最終需要以編程方式調整視圖疊加層,而這些視圖恰好是按鈕,請確保在您的 xml 布局文件中將 stateListAnimator 設置為 null。 stateListAnimator 是 android 的底層過程,用於在單擊按鈕時調整按鈕的 translationZ,因此單擊的按鈕最終會顯示在頂部。 這並不總是您想要的......對於完整的 Z 順序控制,請執行以下操作:

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM