我可以问一下,如何在Android中使用 Viewpager 实现以下布局:
Viewpager和FragmentPagerAdapter都有一个Activity,可以为每个标签返回相应的片段。 我试过以下答案/库:
Custom viewpager tabs with custom view, multiple fragment and layouts
Viewpager in Android with fixed tabs at bottom with Icon and Text in each tab
http://viewpagerindicator.com/
我也尝试在app主题中设置ActionbarStyle,但所有规则都适用于ActionBar,而不是视图寻呼机的标签视图。
<style name="MyTheme" parent="android:Theme.Holo.Light">
<item name="android:actionBarStyle">@style/MyCustomActionBar</item>
<item name="android:actionBarTabStyle">@style/ActionBarTabStyle</item>
<item name="android:actionBarSize">100dp</item>
</style>
<style name="ActionBarTabStyle" parent="@android:style/Widget.Holo.ActionBar.TabView">
<item name="android:minHeight">200dp</item>
</style>
<style name="MyCustomActionBar" parent="@android:style/Widget.Holo.Light.ActionBar">
<item name="android:background">@color/white</item>
<item name="android:height">100dp</item>
</style>
这是viewpager的当前tabview,看起来不太好看:
活动布局:
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
Curent Fragment布局:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/new_deliver_scroll"
>
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:background="@color/background_material_light"
android:id="@+id/newDeliverMain"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/new_deliver_main_linear"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="horizontal"
android:id="@+id/relative_layout_item_info"
>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Item_desc"
android:id="@+id/new_deliver_item_desc_text"
android:textColor="@color/app_blue"
/>
...........
viewPagerAdapter = new SenderViewPagerAdapter( getSupportFragmentManager());
viewPager = (ViewPager) findViewById(R.id.pager);
viewPager.setAdapter( viewPagerAdapter);
actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
viewPager.setOnPageChangeListener(
new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
for (int i = 0; i < viewPagerAdapter.getCount(); i++) {
ActionBar.Tab newTab = actionBar.newTab();
newTab.setCustomView(R.layout.custom_actionbar);
newTab.setTabListener(this);
TextView title = (TextView) newTab.getCustomView().findViewById(R.id.tab_title);
ImageView icon = (ImageView)newTab.getCustomView().findViewById(R.id.tab_icon);
title.setText(viewPagerAdapter.getPageTitle(i));
icon.setImageResource(ICONS[i]);
actionBar.addTab(newTab);
}
经过几次试验,我暂时停留在这里。任何想法,建议如何实现所需的视图将非常感激。非常感谢你。
不适用:
icon.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT));
@refer to @karaokyo回答: 我切换到使用单选按钮而不是操作栏,它工作正常,只需几个自定义。
检查时更改图像:
Drawable top = getResources().getDrawable(R.drawable.image);
button.setCompoundDrawablesWithIntrinsicBounds(null, top , null, null);
对于单选按钮,很难以编程方式更改顶部图像的大小,更好地调整图像本身的大小&gt;带我2个小时搞清楚。
答案 0 :(得分:1)
如果您需要如此多的自定义,则没有理由使用操作栏标签。它也被弃用了,所以就是这样。如果我这样做,我可能会使用RadioGroup
作为标签:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/tabs">
<RadioButton
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawableTop="@drawable/new_delivery"
android:button="@null"
android:text="New Delivery"
android:id="@+id/new_delivery"
android:background="@drawable/checked_background_indicator"
android:checked="true"
style="?android:attr/borderlessButtonStyle"/>
<RadioButton
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawableTop="@drawable/job_history"
android:button="@null"
android:text="Job History"
android:id="@+id/job_history"
android:background="@drawable/checked_background_indicator"
style="?android:attr/borderlessButtonStyle"/>
<RadioButton
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawableTop="@drawable/track_delivery"
android:button="@null"
android:text="Track Delivery"
android:id="@+id/track_delivery"
android:background="@drawable/checked_background_indicator"
style="?android:attr/borderlessButtonStyle"/>
</RadioGroup>
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/tabs"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
checked_background_indicator.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="@color/accent" />
</selector>
然后将它们与听众联系在一起:
mTabs.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.new_delivery:
mViewPager.setCurrentItem(0);
break;
case R.id.job_history:
mViewPager.setCurrentItem(1);
break;
case R.id.track_delivery:
mViewPager.setCurrentItem(2);
}
}
});
mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
switch(position){
case 0:
mTabs.check(R.id.new_delivery);
break;
case 1:
mTabs.check(R.id.job_history);
break;
case 2:
mTabs.check(R.id.track_delivery);
break;
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});