【发布时间】:2017-07-27 13:48:00
【问题描述】:
背景
假设您有一个滚动活动,其中包含 CoordinatorLayout 和 AppBarLayout,其中您有一个顶部标题,可以在底部滚动时折叠(可能有 RecyclerView 或 NestedScrollView)。
类似这样的:
现在,您需要在内容出现之前,在底部区域(显示文本的位置)的中心放置一个加载视图(例如,一个 ProgressBar 和一个 LinearLayout 内的 TextView)。
问题
我使用 ViewAnimator 在状态之间切换,但是如果我选择将加载视图居中,它会显示在中心下方,因为底部区域占用了更多实际显示的空间(因为您可以滚动它)。
我尝试了什么
我执行的 2 个解决方案效果很好: 1.获取加载视图及其父级的大小,并用它计算加载视图的上边距 2. 将加载视图的底部填充设置为上部区域大小的一半(本例中为 90dp,即上部区域 180dp 的一半)。这有点容易,但如果 Activity 中的任何 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,所以计算父级的东西有点奇怪用于此目的的这些片段中的活动。
不仅如此,我还在底部有一个tabLayout(属于活动),这使得考虑起来更加烦人。
【问题讨论】:
-
我没有检查代码,因为我很着急……你使用了重力属性/layout_centerVertical 吗?或 layout_centerInParent ?抱歉,您的回复很垃圾..
-
@IonutJ.Bejan 是的,它在上面的代码中。居中是整个布局,只有滚动后才能看到整体(上部区域的折叠模式),所以你会看到我在第二张截图中得到了什么。
标签: android android-collapsingtoolbarlayout android-appbarlayout