首页 > 解决方案 > 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>

标签: androidandroid-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"/>

推荐阅读