[英]Android overlay a view ontop of everything?
你能在 android 中的所有东西上叠加一个视图吗?
在iPhone我会得到新的视图设置其frame.origin
到(0,0),其宽度和高度与宽度和高度self.view
。 将它添加到self.view
会导致它作为一个覆盖层,覆盖后面的内容(或者如果它有一个透明的背景然后显示后面的视图)。
android中是否有类似的技术? 我意识到视图略有不同(有三种类型(或更多......)相对布局、线性布局和框架布局)但是有什么方法可以不加选择地将视图覆盖在所有内容之上?
只需使用RelativeLayout
或FrameLayout
。 最后一个子视图将覆盖其他所有内容。
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 中重新编码的原因。 这是有关如何在Java和Kotlin 中使用它的文档。 首先,从要显示叠加层的活动中对其进行初始化-
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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.