我正在使用TabLayout设计库我想要实现的是
我已经尝试了很多教程,我可以通过自定义选项卡来实现它,但是当选择选项卡时会出现限制我想要更改文本颜色以及图标的图像,这是通过引用无法实现的到目前为止我读过的任何教程.到目前为止,我已经尝试过这个了FragmentStatePagerAdapter
public View getTabView(int position) {
View tab = LayoutInflater.from(mContext).inflate(R.layout.tabbar_view, null);
TextView tabText = (TextView) tab.findViewById(R.id.tabText);
ImageView tabImage = (ImageView) tab.findViewById(R.id.tabImage);
tabText.setText(mFragmentTitles.get(position));
tabImage.setBackgroundResource(mFragmentIcons.get(position));
if (position == 0) {
tab.setSelected(true);
}
return tab;
}
Run Code Online (Sandbox Code Playgroud) 我想将片段替换为TabLayout Viewpager包含的另一个片段.像这样:
活动 - > 片段 - > B片段(TabLayout ViewPager) - > C片段(Tab片段) - > D片段
我的任务就是转C片段到片段d.
注意: - TabLayout ViewPager还包含3个选项卡Fragment控件FragmentPagerAdapter.
Tablayout片段的布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<android.support.design.widget.TabLayout
android:id="@+id/tablayout_postpaid_service"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
app:tabGravity="fill"
app:tabIndicatorColor="@color/white"
app:tabMode="scrollable"
app:tabSelectedTextColor="@color/white"
app:tabTextColor="@color/tab_fade_text_color" >
</android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager_postpaid_service"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- <FrameLayout
android:id="@+id/container_body"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" /> -->
</android.support.v4.view.ViewPager>
Run Code Online (Sandbox Code Playgroud)
片段B类:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_select_postpaid_service, container, false);
tablayout …Run Code Online (Sandbox Code Playgroud) android android-layout android-fragments android-viewpager android-tablayout
嗨,我正在使用和制作一个带有A fragment和少数孩子的应用程序.问题是我的孩子(从)总是执行(每当用户点击父母时加载所有代码.如何让孩子只在用户滑动时加载(例如我有2个标签,仅在标签2中的内容)用户滑动到标签2时加载fragmenttablayoutviewpagerfragmentTablayoutfragmentfragment
这是我的父fragment代码
public class ManageEventFragment extends Fragment {
public ManageEventFragment() {
// Required empty public constructor
}
TabLayout tabLayout;
ViewPager viewPager;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View viewFragment = inflater.inflate(R.layout.fragment_manage_event, container, false);
viewPager = (ViewPager) viewFragment.findViewById(R.id.viewPager);
tabLayout = (TabLayout) viewFragment.findViewById(R.id.tabLayout);
viewPager.setAdapter(new CustomAdapter(getChildFragmentManager(), getContext()));
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
return viewFragment;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// tabLayout.setupWithViewPager(viewPager);
tabLayout.post(new Runnable() {
@Override
public void run() { …Run Code Online (Sandbox Code Playgroud) android android-fragments android-viewpager android-tablayout
我想用TabLayout用TabItem用下面的代码:
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/color_primary"
app:tabGravity="fill"
app:tabIndicatorColor="@color/white"
app:tabMode="fixed"
app:tabSelectedTextColor="@color/white"
app:tabTextColor="@color/gray_light">
<android.support.design.widget.TabItem
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:icon="@drawable/ic_language" />
<!-- other tabs ... -->
</android.support.design.widget.TabLayout>
Run Code Online (Sandbox Code Playgroud)
这是正确的向我展示图标:
但问题是当我想添加我TabLayout的时候ViewPager,使用以下代码,所有这些代码都存在且可点击但却消失了.我错过了什么?
MyPagerAdapter adapter = new MyPagerAdapter(getFragmentManager());
pager.setAdapter(adapter);
tabLayout.setupWithViewPager(pager);
Run Code Online (Sandbox Code Playgroud)
这是结果:
android material-design android-support-design android-tablayout
我按照这个创建了一个Android Radio Stream
它在那里工作正常所以我试图添加一个标签布局
所以我按照这个添加Tab布局
在那里我已经添加但是我有很多错误所以我改变了一些像
从
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
Run Code Online (Sandbox Code Playgroud)
对此
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View viewRoot = inflater.inflate(R.layout.main_activity, container, false);
Run Code Online (Sandbox Code Playgroud)
并且viewRoot.findViewById在一些地方添加on-create但是在On之后创造了我曾经拥有的东西getActivity().findViewById
因为我已经return viewRoot;在onCreate中给出 了它是不可能的
现在我收到了错误 play.setBackground(getResources().getDrawable(R.drawable.volume0));
喜欢
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setBackground(android.graphics.drawable.Drawable)' on a null object reference
at star.yx.tabview.ProfileFragment.updatePlayStatus(ProfileFragment.java:370)
at star.yx.tabview.ProfileFragment.setIFace(ProfileFragment.java:205)
at star.yx.tabview.ProfileFragment.onCreateView(ProfileFragment.java:158)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2184)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1298)
at android.support.v4.app.FragmentManagerImpl.moveFragmentsToInvisible(FragmentManager.java:2323)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2136)
at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2092)
at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:1969) …Run Code Online (Sandbox Code Playgroud) 我目前正在开发 v22 的应用程序并使用以下库:
compile 'com.android.support:appcompat-v7:22.2.1'
compile 'com.android.support:support-v4:22.2.1'
compile 'com.android.support:design:22.2.1'
Run Code Online (Sandbox Code Playgroud)
我只有一个名为HomeActivity 的活动,里面有一个导航抽屉。这是activity_home.xml布局:
<android.support.v4.widget.DrawerLayout
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:id="@+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="false">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/ThemeOverlay.AppCompat.ActionBar" />
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
/>
</android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.NavigationView
android:id="@+id/navigation_view"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:layout_gravity="start"
app:menu="@menu/menu_navigation"/>
Run Code Online (Sandbox Code Playgroud)
因此,每次我从导航抽屉中选择一个项目时,我都会@+id/content_frame用一个新的 Fragment替换 FrameView ,如下所示:
int id = menuItem.getItemId();
switch (id)
{
case R.id.gazzette:
fragmentManager.beginTransaction()
.replace(R.id.content_frame, new ListViewFragment())
.commit();
drawerLayout.closeDrawers();
break;
case R.id.concorsi: …Run Code Online (Sandbox Code Playgroud) android fragment material-design android-elevation android-tablayout
我想要没有填充的 TabLayout 选项卡。和 tabPaddingStart , tabPaddingEnd 正在工作。但是 tabPaddingBottom 和 tabPaddingTop 不起作用。我将它们的值更改为 -1 和 0,但我的项目没有变化。
我想问的另一件事是在我的标签处于糟糕状态后屏幕旋转。TextView 未完全显示。和 ImageView .....
这是我的活动
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:design="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="10">
<android.support.v4.view.ViewPager
android:id="@+id/view_pager_main_content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="9">
</android.support.v4.view.ViewPager>
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:visibility="visible"
android:layout_weight="1"
design:tabBackground="@drawable/tab_color_selector"
design:tabIndicatorColor="@color/colorPrimary"
design:tabMaxWidth="0dp"
design:tabPaddingStart="0dp" // working
design:tabPaddingEnd="0dp" // working
design:tabPaddingBottom="0dp"
design:tabPaddingTop="0dp"
design:tabContentStart="0dp"
design:tabGravity="fill"
design:tabMode="fixed"
>
</android.support.design.widget.TabLayout>
</LinearLayout>
//Tab Layout selector
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/colorPrimaryTransparent"
android:state_selected="true"></item>
<item android:drawable="@color/white"
android:state_selected="false"></item>
</selector>
Run Code Online (Sandbox Code Playgroud)
“我的 tabLayout 自定义选项卡样式”
<?xml version="1.0" encoding="utf-8"?> …Run Code Online (Sandbox Code Playgroud) So my app is in RTL, and with that, the tabs are now ordered from right to left..!
..But when I swipe to slide between the tabs, it contradicts the common sense, so I want to reverse it..! see the picture to see the problem..!
What I want is..! > when I'm in Tab1 and then I swipe from left to right I want it to slide to Tab 2 and so on..!
is that possible..!?
THE CODE
My Customer …
android android-fragments swipe-gesture android-viewpager android-tablayout