android - Android创建全屏相对布局
问题描述
我想我已经阅读了与此相关的所有 SO 问题,但仍然没有找到解决这个简单需求的好方法......
我正在创建几个自定义 RelativeLayouts 并以编程方式将它们添加到我的窗口中。它们工作得很好,除了它们不覆盖状态栏。当我想显示其中一个时,我试图隐藏状态栏,但当然隐藏是动画的,我想避免。
注意:我尝试在下面的示例中使用“WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY 而不是 TYPE_PHONE。这确实覆盖了状态栏,但它不接受我需要的触摸事件。
感谢您的任何建议!
这是我的自定义RelativeLayout:
public class MyView extends RelativeLayout {
public MyView(Context context) {
super(context);
init(context, null, 0);
}
public MyView(Context context, AttributeSet attrs) {
super(context,attrs);
init(context, attrs, 0);
}
public MyView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context, attrs, defStyle);
}
private void init(Context context, AttributeSet attrs, int defStyle)
{
final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
PixelFormat.TRANSLUCENT);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.my_layout, this, true);
this.setVisibility(View.GONE);
WindowManager windowManager = (WindowManager) activity.getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
windowManager.addView(this, params);
}
....
这是我的布局文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/my_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="false"
android:background="#000000"
android:focusable="true"
android:clickable="true"
android:focusableInTouchMode="true">
<ImageView
android:id="@+id/myImageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:focusable="true"
android:clickable="true"
android:focusableInTouchMode="true"
/>
</RelativeLayout>
解决方案
以全屏模式运行视图。
public class ActivityName extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// remove title
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
}
}
在 AppCompatActivity 的情况下,使用它。
<activity android:name=".ActivityName"
android:label="@string/app_name"
android:theme="@style/Theme.AppCompat.Light.NoActionBar"/>
推荐阅读
- excel - VBA /复制列的最大值和同一行和上一列中的日期并粘贴到新工作表中
- javascript - 想要减小 JSON 字符串的大小
- cookies - Next.js 不持久化 cookie
- mysql - 没有重复列时收到错误代码 1062
- javascript - 数据表不适用于实时更新数据
- javascript - 在点击功能已经存在的情况下,难以在 UL 中更改 IMG Src
- function - 当我追加函数调用时,为什么函数执行顺序似乎颠倒了?
- docker - 错误:合并到主分支后缺少绑定“binding.node”
- c# - 如何将字典值从控制器传递到视图?
- node.js - Mongoose - `CastError: Cast to number failed` 使用“$fieldName”时