简体   繁体   English

Android创建全屏相对布局

[英]Android create full screen relative layout

I think I've read every SO question related to this but still haven't found a good solution to this simple need... 我想我已经阅读了与此相关的每个SO问题,但仍然没有找到解决这种简单需求的好方法...

I'm creating several custom RelativeLayouts and adding them to my window programmatically. 我正在创建几个自定义RelativeLayouts并将它们以编程方式添加到我的窗口中。 They work great, except that they don't cover the status bar. 它们工作得很好,除了它们不覆盖状态栏。 I've tried to just hide the status bar when I want to show one of them, but of course the hiding is animated which I'd like to avoid. 当我想显示状态栏之一时,我试图只隐藏状态栏,但是当然,隐藏起来是动画化的,我想避免这种情况。

Note: I've tried using "WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY instead of TYPE_PHONE in the example below. That does cover the status bar, but then it does't accept touch events which I need. 注意:在下面的示例中,我尝试使用“ WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY”而不是TYPE_PHONE。它确实覆盖了状态栏,但是它不接受我需要的触摸事件。

Thanks for any suggestions! 感谢您的任何建议!

Here is my custom RelativeLayout: 这是我的自定义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);
}

....

Here is my layout file: 这是我的布局文件:

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

To run the view in fullscreen mode. 以全屏模式运行视图。

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

In case of AppCompatActivity, use this. 如果是AppCompatActivity,请使用它。

<activity android:name=".ActivityName"
android:label="@string/app_name"
android:theme="@style/Theme.AppCompat.Light.NoActionBar"/>

Well I tried to show a full screen-activity from my existing activity without animation, and even that animates the hiding of the action bar. 好吧,我试图在没有动画的情况下从我现有的活动中显示全屏活动,甚至动画化了动作栏的隐藏。 It's also a little laggy. 这也有点落后。

I'm back to creating custom RelativeLayouts per my question above, then showing them as needed. 我要根据上面的问题创建自定义RelativeLayouts,然后根据需要显示它们。 (Only I have to hide the action bar a second before I want to show display a RelativeLayout.) (仅当我想显示RelativeLayout时,才必须隐藏操作栏一秒钟。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM