背景
假设您有一个可滚动的活动,带有CoordinatorLayout和AppBarLayout,在其中您有一个顶部标题栏,该标题栏可以在底部滚动时折叠(可能具有RecyclerView或NestedScrollView)。
类似于这样:
现在,您需要在内容出现之前在底部区域的中心放置一个加载视图(例如,一个LinearLayout中的ProgressBar和TextView)。
问题
我使用ViewAnimator来切换状态,但如果我选择将加载视图居中,则会显示在中心下方,因为底部区域占用的空间比实际显示的空间更大(因为您可以滚动它)。
我的解决方法
我尝试了两个有效的解决方法: 1. 获取加载视图和其父级的大小,并使用它来计算加载视图的上边距 2. 将加载视图的底部填充设置为上部区域大小的一半(在此情况下为90dp,这是上部区域180dp的一半)。这种方法稍微简单一些,但如果活动中的任何UI或底部区域的父级发生更改,则会破坏居中的修复。
代码
与IDE向导中的代码几乎相同,但这里:
ScrollingActivity.java
public class ScrollingActivity extends AppCompatActivity {
private ViewAnimator mViewAnimator;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scrolling);
setSupportActionBar((Toolbar) findViewById(R.id.toolbar));
mViewAnimator = findViewById(R.id.viewAnimator);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_scrolling, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
mViewAnimator.showNext();
return true;
}
return super.onOptionsItemSelected(item);
}
}
res/layout/activity_scrolling.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:fitsSystemWindows="true"
tools:context="com.example.user.myapplication.ScrollingActivity">
<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar" android:layout_width="match_parent" android:layout_height="@dimen/app_bar_height"
android:fitsSystemWindows="true" android:theme="@style/AppTheme.AppBarOverlay">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/toolbar_layout" android:layout_width="match_parent" android:layout_height="match_parent"
android:fitsSystemWindows="true" app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed" app:toolbarId="@+id/toolbar">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar" android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize" app:layout_collapseMode="pin"
app:popupTheme="@style/AppTheme.PopupOverlay"/>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<ViewAnimator
android:id="@+id/viewAnimator" android:layout_width="match_parent" android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<LinearLayout
android:id="@+id/loader" android:layout_width="match_parent" android:layout_height="match_parent"
android:gravity="center" android:orientation="vertical">
<ProgressBar
android:layout_width="wrap_content" android:layout_height="wrap_content"/>
<TextView
android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="loading"/>
</LinearLayout>
<android.support.v4.widget.NestedScrollView
android:id="@+id/contentView" android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_margin="@dimen/text_margin" android:text="@string/large_text"/>
</android.support.v4.widget.NestedScrollView>
</ViewAnimator>
</android.support.design.widget.CoordinatorLayout>
res/menu/menu_scrolling.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" tools:context="com.example.user.myapplication.ScrollingActivity">
<item
android:id="@+id/action_settings" android:orderInCategory="100" android:title="click"
app:showAsAction="always"/>
</menu>
问题
我知道如何自己计算,但我想知道是否有其他简单的方法。也许有一些我不知道的标志?
我问这个问题是因为真实应用程序中的底部区域更加复杂(上面的示例仅演示问题),具有多个片段甚至带有 TabLayout&ViewPager 的多个片段,因此在这些片段中计算父 Activity 的内容有些奇怪。
不仅如此,我还在底部有一个 tabLayout(属于 activity),这使得考虑更加麻烦。