如何正确制作 activity 全屏 - 尝试隐藏所有栏时的视图问题
How to properly make an activity FullScreen - Issues with Views when trying to hide all bars
我有一个简单的 activity 想要全屏显示 - 删除状态栏、操作栏和标题栏。
我有几个问题:
- 在 'hide' 操作期间,当 activity 启动时 - 有明显的滞后。似乎首先它隐藏了 ActionBar,然后才完全从布局中删除。
我不介意,但是这个操作很明显,隐藏阶段和移除阶段之间有一个滞后。
如何消除延迟?
- 我希望 activity 在恢复时也处于全屏状态(例如,转到另一个应用程序,然后从
Recent
视图返回此 activity)。问题是,当我实现 onResume()
进行隐藏时 - activity 表现得很奇怪 - TextView 没有显示,ImageView 突然没有响应点击。
当 完全没有 使用 onResume()
方法时 - 正如预期的那样,一切正常。
我的Activity代码:
public class ListItemActivity extends AppCompatActivity implements View.OnClickListener {
private final static String TAG = "ListItemActivity";
private ScrollView mSvLyrics;
private TextView mTvLyrics;
private TextView mErrorMessageDisplay;
private ProgressBar mLoadingIndicator;
/**
* Shows error message view while hiding other results related views
*/
private void showErrorMessage(String s) {
mLoadingIndicator.setVisibility(View.INVISIBLE);
mSvLyrics.setVisibility(View.INVISIBLE);
mErrorMessageDisplay.setText(s);
mErrorMessageDisplay.setVisibility(View.VISIBLE);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);
getSupportActionBar().hide();
setContentView(R.layout.activity_list_item);
/* Set click listener for close image */
findViewById(R.id.iv_close).setOnClickListener(this);
/* Init fields and needed params */
mSvLyrics = findViewById(R.id.sv_lyrics);
mTvLyrics = findViewById(R.id.tv_lyrics);
mErrorMessageDisplay = findViewById(R.id.tv_item_error_message_display);
mLoadingIndicator = findViewById(R.id.pb_item_loading_indicator);
final Intent intent = getIntent();
/* Set Track properties in the views */
((TextView)findViewById(R.id.tv_item_artist)).setText(intent.getStringExtra(LyricsAPI.KEY_ARTIST_NAME));
((TextView)findViewById(R.id.tv_item_title)).setText(intent.getStringExtra(LyricsAPI.KEY_TRACK_NAME));
/* Fetch Lyrics for the track */
URL lyricsUrl = AidUtils.buildUrl(intent.getStringExtra(LyricsAPI.KEY_TRACK_ID), LyricsAPI.RequestType.TRACK_LYRICS_GET);
new AsyncFetchLyrics().execute(lyricsUrl);
}
@Override
protected void onResume() {
super.onResume();
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);
getSupportActionBar().hide();
setContentView(R.layout.activity_list_item);
}
@Override
public void onClick(View v) {
finish();
}
public class AsyncFetchLyrics extends AsyncTask<URL, Void, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
mErrorMessageDisplay.setVisibility(View.INVISIBLE);
mSvLyrics.setVisibility(View.INVISIBLE);
mLoadingIndicator.setVisibility(View.VISIBLE);
}
@Override
protected String doInBackground(URL... urls) {
URL url = urls[0];
String response = null;
try {
response = AidUtils.getResponseFromHttpUrl(url);
} catch (IOException e) {
e.printStackTrace();
return null;
}
String lyrics = null;
try {
lyrics = AidUtils.getLyricsFromJsonStr(response);
} catch (LyricsAPIException e) {
Log.e(TAG, "Failed to fetch data from lyrics API", e);
}
return lyrics;
}
@Override
protected void onPostExecute(String lyrics) {
if(null == lyrics) {
showErrorMessage(getString(R.string.error_message));
return;
}
if(lyrics.isEmpty()) {
showErrorMessage(getString(R.string.no_results));
return;
}
mLoadingIndicator.setVisibility(View.INVISIBLE);
mErrorMessageDisplay.setVisibility(View.INVISIBLE);
mTvLyrics.setText(lyrics);
mSvLyrics.setVisibility(View.VISIBLE);
}
}
}
我的XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="horizontal"
android:padding="0dp"
android:gravity="center">
<ImageView
android:id="@+id/iv_close"
android:layout_width="40dp"
android:layout_height="40dp"
android:adjustViewBounds="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
android:src="@android:drawable/ic_menu_close_clear_cancel"
android:scaleType="fitCenter"
android:clickable="true"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="30dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="@+id/tv_item_artist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/example_text"
android:textSize="35sp"
android:textStyle="bold"
android:textColor="@color/app_text_color"
android:textAlignment="center"
android:maxLines="2"
android:ellipsize="end"
android:padding="5dp"
android:background="@android:color/transparent">
</TextView>
<TextView
android:id="@+id/tv_item_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/example_text"
android:textSize="30sp"
android:textAlignment="center"
android:textColor="@color/app_text_color"
android:maxLines="1"
android:ellipsize="end"
android:padding="5dp"
android:background="@android:color/transparent">
</TextView>
</LinearLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:id="@+id/sv_lyrics"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:fillViewport="true"
android:layout_marginTop="40dp"
android:layout_marginBottom="25dp"
android:layout_marginRight="20dp"
android:layout_marginLeft="20dp"
android:visibility="invisible">
<TextView
android:id="@+id/tv_lyrics"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:textStyle="italic"
android:fontFamily="monospace"
android:text="@string/example_text"
android:textAlignment="center"
android:textSize="16sp"/>
</ScrollView>
<TextView
android:id="@+id/tv_item_error_message_display"
android:textSize="20sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:padding="20dp"
android:text="@string/error_message"
android:textStyle="italic"
android:textColor="@android:color/holo_red_light"
android:visibility="invisible" />
<ProgressBar
android:id="@+id/pb_item_loading_indicator"
android:layout_height="42dp"
android:layout_width="42dp"
android:layout_gravity="center_vertical|center_horizontal"
android:visibility="invisible" />
</FrameLayout>
</LinearLayout>
</RelativeLayout>
AndroidManifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.bilyrics"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="26" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:debuggable="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme" >
<activity android:name="com.example.android.bilyrics.MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.android.bilyrics.ListItemActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:theme="@android:style/Theme.AppCompat.Light.NoActionBar" >
</activity>
<meta-data
android:name="android.support.VERSION"
android:value="26.1.0" />
<meta-data
android:name="android.arch.lifecycle.VERSION"
android:value="27.0.0-SNAPSHOT" />
</application>
</manifest>
您可以通过应用 activity 的主题来实现。
在style.xml
<style name="FullScreenTheme"
parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowNoTitle">true</item>
<item name="android:windowActionBar">false</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">@null</item>
</style>
并且在manifest.xml
<activity android:name=".MyActivity"
android:label="@string/app_name"
android:theme="@style/FullScreenTheme"/>
制作全屏 Activity 的最简单方法是使用 OnCreate
中的代码:
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
我有一个简单的 activity 想要全屏显示 - 删除状态栏、操作栏和标题栏。
我有几个问题:
- 在 'hide' 操作期间,当 activity 启动时 - 有明显的滞后。似乎首先它隐藏了 ActionBar,然后才完全从布局中删除。 我不介意,但是这个操作很明显,隐藏阶段和移除阶段之间有一个滞后。 如何消除延迟?
- 我希望 activity 在恢复时也处于全屏状态(例如,转到另一个应用程序,然后从
Recent
视图返回此 activity)。问题是,当我实现onResume()
进行隐藏时 - activity 表现得很奇怪 - TextView 没有显示,ImageView 突然没有响应点击。 当 完全没有 使用onResume()
方法时 - 正如预期的那样,一切正常。
我的Activity代码:
public class ListItemActivity extends AppCompatActivity implements View.OnClickListener {
private final static String TAG = "ListItemActivity";
private ScrollView mSvLyrics;
private TextView mTvLyrics;
private TextView mErrorMessageDisplay;
private ProgressBar mLoadingIndicator;
/**
* Shows error message view while hiding other results related views
*/
private void showErrorMessage(String s) {
mLoadingIndicator.setVisibility(View.INVISIBLE);
mSvLyrics.setVisibility(View.INVISIBLE);
mErrorMessageDisplay.setText(s);
mErrorMessageDisplay.setVisibility(View.VISIBLE);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);
getSupportActionBar().hide();
setContentView(R.layout.activity_list_item);
/* Set click listener for close image */
findViewById(R.id.iv_close).setOnClickListener(this);
/* Init fields and needed params */
mSvLyrics = findViewById(R.id.sv_lyrics);
mTvLyrics = findViewById(R.id.tv_lyrics);
mErrorMessageDisplay = findViewById(R.id.tv_item_error_message_display);
mLoadingIndicator = findViewById(R.id.pb_item_loading_indicator);
final Intent intent = getIntent();
/* Set Track properties in the views */
((TextView)findViewById(R.id.tv_item_artist)).setText(intent.getStringExtra(LyricsAPI.KEY_ARTIST_NAME));
((TextView)findViewById(R.id.tv_item_title)).setText(intent.getStringExtra(LyricsAPI.KEY_TRACK_NAME));
/* Fetch Lyrics for the track */
URL lyricsUrl = AidUtils.buildUrl(intent.getStringExtra(LyricsAPI.KEY_TRACK_ID), LyricsAPI.RequestType.TRACK_LYRICS_GET);
new AsyncFetchLyrics().execute(lyricsUrl);
}
@Override
protected void onResume() {
super.onResume();
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);
getSupportActionBar().hide();
setContentView(R.layout.activity_list_item);
}
@Override
public void onClick(View v) {
finish();
}
public class AsyncFetchLyrics extends AsyncTask<URL, Void, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
mErrorMessageDisplay.setVisibility(View.INVISIBLE);
mSvLyrics.setVisibility(View.INVISIBLE);
mLoadingIndicator.setVisibility(View.VISIBLE);
}
@Override
protected String doInBackground(URL... urls) {
URL url = urls[0];
String response = null;
try {
response = AidUtils.getResponseFromHttpUrl(url);
} catch (IOException e) {
e.printStackTrace();
return null;
}
String lyrics = null;
try {
lyrics = AidUtils.getLyricsFromJsonStr(response);
} catch (LyricsAPIException e) {
Log.e(TAG, "Failed to fetch data from lyrics API", e);
}
return lyrics;
}
@Override
protected void onPostExecute(String lyrics) {
if(null == lyrics) {
showErrorMessage(getString(R.string.error_message));
return;
}
if(lyrics.isEmpty()) {
showErrorMessage(getString(R.string.no_results));
return;
}
mLoadingIndicator.setVisibility(View.INVISIBLE);
mErrorMessageDisplay.setVisibility(View.INVISIBLE);
mTvLyrics.setText(lyrics);
mSvLyrics.setVisibility(View.VISIBLE);
}
}
}
我的XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="horizontal"
android:padding="0dp"
android:gravity="center">
<ImageView
android:id="@+id/iv_close"
android:layout_width="40dp"
android:layout_height="40dp"
android:adjustViewBounds="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
android:src="@android:drawable/ic_menu_close_clear_cancel"
android:scaleType="fitCenter"
android:clickable="true"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="30dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="@+id/tv_item_artist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/example_text"
android:textSize="35sp"
android:textStyle="bold"
android:textColor="@color/app_text_color"
android:textAlignment="center"
android:maxLines="2"
android:ellipsize="end"
android:padding="5dp"
android:background="@android:color/transparent">
</TextView>
<TextView
android:id="@+id/tv_item_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/example_text"
android:textSize="30sp"
android:textAlignment="center"
android:textColor="@color/app_text_color"
android:maxLines="1"
android:ellipsize="end"
android:padding="5dp"
android:background="@android:color/transparent">
</TextView>
</LinearLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:id="@+id/sv_lyrics"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:fillViewport="true"
android:layout_marginTop="40dp"
android:layout_marginBottom="25dp"
android:layout_marginRight="20dp"
android:layout_marginLeft="20dp"
android:visibility="invisible">
<TextView
android:id="@+id/tv_lyrics"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:textStyle="italic"
android:fontFamily="monospace"
android:text="@string/example_text"
android:textAlignment="center"
android:textSize="16sp"/>
</ScrollView>
<TextView
android:id="@+id/tv_item_error_message_display"
android:textSize="20sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:padding="20dp"
android:text="@string/error_message"
android:textStyle="italic"
android:textColor="@android:color/holo_red_light"
android:visibility="invisible" />
<ProgressBar
android:id="@+id/pb_item_loading_indicator"
android:layout_height="42dp"
android:layout_width="42dp"
android:layout_gravity="center_vertical|center_horizontal"
android:visibility="invisible" />
</FrameLayout>
</LinearLayout>
</RelativeLayout>
AndroidManifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.bilyrics"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="26" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:debuggable="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme" >
<activity android:name="com.example.android.bilyrics.MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.android.bilyrics.ListItemActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:theme="@android:style/Theme.AppCompat.Light.NoActionBar" >
</activity>
<meta-data
android:name="android.support.VERSION"
android:value="26.1.0" />
<meta-data
android:name="android.arch.lifecycle.VERSION"
android:value="27.0.0-SNAPSHOT" />
</application>
</manifest>
您可以通过应用 activity 的主题来实现。
在style.xml
<style name="FullScreenTheme"
parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowNoTitle">true</item>
<item name="android:windowActionBar">false</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">@null</item>
</style>
并且在manifest.xml
<activity android:name=".MyActivity"
android:label="@string/app_name"
android:theme="@style/FullScreenTheme"/>
制作全屏 Activity 的最简单方法是使用 OnCreate
中的代码:
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);