【发布时间】:2014-01-04 12:00:28
【问题描述】:
我已经按照in the android documentation.的指示在我的应用中实现了一个导航抽屉
一切正常,但由于我的一些片段正在使用标签,我想在抽屉展开的同时“滚动”tabviews。如这里所示:
我的actionBarToggle 中的 onDrawerSlide() 方法非常适合它,因为它传递了抽屉展开距离的百分比。
我在actionBarToggle 的onDrawerOpened() 和onDrawerClosed() 方法中更改了操作栏的导航模式,一切正常。但我想要某种动画,因为它现在看起来很粗暴。
[抽屉打开 - puff 标签消失了puff;抽屉关闭 - puff 标签又出现了 puff]
我当前的代码。
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerToggle =
new ActionBarDrawerToggle(this, drawerLayout, R.drawable.ic_drawer,
R.string.navigation, R.string.closed) {
public void onDrawerClosed(View view) {
supportInvalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
setTitle(getString(R.string.navigation));
setIcon(R.drawable.abc_ic_search_api_holo_light);
supportInvalidateOptionsMenu();
}
@Override
public void onDrawerSlide(View drawerView, float slideOffset) {
// TODO animation
super.onDrawerSlide(drawerView, slideOffset);
}
};
我希望得到诸如获取 tabhost/view 之类的东西并根据 slideOffset 设置它的高度。
@Override
public void onDrawerSlide(View drawerView, float slideOffset) {
TabView tabView = actionBar.getTabView(); // how do I get the view where the tabs are shown?
ViewGroup.LayoutParams tabViewLayoutParams = tabView.getLayoutParams();
ViewGroup.LayoutParams tabViewNewLayoutParams = new ViewGroup.LayoutParams(a.width, (int)((1 - slideOffset) * normalTabHeight))
drawer.setLayoutParams(tabViewNewLayoutParams );
super.onDrawerSlide(drawerView, slideOffset);
}
如何获得view 所在的tabs?
我正在使用支持库 API 7+ 和 SupportActionBar。
“tabhost”是如何包含在操作栏中的?是否有可能获得这个“tabview/host”并以编程方式在运行时更改它的高度?
【问题讨论】:
-
在 support v7 库中似乎没有为标签容器分配任何 ID。您可以通过手动浏览容器层次结构来找到孩子,但将来可能会发生变化。另请注意,这些选项卡可以出现在宽屏和纵向模式下的顶部操作栏内。
-
@corsair992 由于 ActionBar.Tab 没有扩展 View 我不能调用 getParent() 或者有其他方法来获取容器吗?我知道这件事。 “动画”应仅适用于选项卡在图像中显示的设备和情况。不过感谢您的回复。 :)
-
这里有一些代码可以从它在层次结构中的当前位置获取标签容器:
((ViewGroup)getWindow().findViewById(Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB ? android.support.v7.appcompat.R.id.action_bar_container : Resources.getSystem().getIdentifier("action_bar_container", "id", "android"))).getChildAt(2);请注意,如果容器包含在顶部操作栏中,这将返回 null。 -
@corsair992 感谢您提供代码 sn-p。 :) 这实际上有效并返回所需的视图。但我不能改变它的高度。更改布局参数似乎根本不起作用。但这是正确的视图,因为我更改了背景颜色并且它起作用了。你知道如何做到这一点吗?
-
更改
LayoutParams尺寸不会影响任何内容,直到下一个布局。您可以在API guides 中了解如何实现View动画。
标签: java android animation android-actionbar android-tabhost