简体   繁体   中英

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

I'm creating several custom RelativeLayouts and adding them to my window programmatically. 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.

Thanks for any suggestions!

Here is my custom 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.

<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. (Only I have to hide the action bar a second before I want to show display a RelativeLayout.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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