如何在操作栏中移动标题选项卡指示器?
How to Move Title Tab Indicator in Action bar?
在操作栏中添加过多选项卡后,选项卡标题文本变小且彼此靠近。我试图更改操作栏的宽度,但没有帮助。请帮助我,对不起我的语言不好。这是我到目前为止的编码,
MainActivity.java
package com.topitideas.dailymasnoonduain;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setText("Home"));
tabLayout.addTab(tabLayout.newTab().setText("Page 1"));
tabLayout.addTab(tabLayout.newTab().setText("Page 2"));
tabLayout.addTab(tabLayout.newTab().setText("Page 3"));
tabLayout.addTab(tabLayout.newTab().setText("Page 4"));
tabLayout.addTab(tabLayout.newTab().setText("Page 5"));
tabLayout.addTab(tabLayout.newTab().setText("Page 6"));
tabLayout.addTab(tabLayout.newTab().setText("Page 7"));
tabLayout.addTab(tabLayout.newTab().setText("Page 8"));
tabLayout.addTab(tabLayout.newTab().setText("Page 9"));
tabLayout.addTab(tabLayout.newTab().setText("Page 10"));
tabLayout.addTab(tabLayout.newTab().setText("Page 11"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
final PagerAdapter adapter = new PagerAdapter
(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
activity_main.xml
<RelativeLayout
android:id="@+id/main_layout"
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"
tools:context=".MainActivity">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="?attr/colorPrimary"
android:elevation="6dp"
android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/toolbar"
android:background="?attr/colorPrimaryDark"
android:elevation="6dp"
android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_below="@id/tab_layout"/>
</RelativeLayout>
PagerAdapter.java
package com.topitideas.dailymasnoonduain;
/**
* Created by Saeed on 11/9/2015.
*/
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
public class PagerAdapter extends FragmentStatePagerAdapter {
int mNumOfTabs;
public PagerAdapter(FragmentManager fm, int NumOfTabs) {
super(fm);
this.mNumOfTabs = NumOfTabs;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
Home tab1 = new Home();
return tab1;
case 1:
Page1 tab2 = new Page1();
return tab2;
case 2:
Page2 tab3 = new Page2();
return tab3;
case 3:
Page3 tab4 = new Page3();
return tab4;
case 4:
Page4 tab5 = new Page4();
return tab5;
case 5:
Page5 tab6 = new Page5();
return tab6;
case 6:
Page6 tab7 = new Page6();
return tab7;
case 7:
Page7 tab8 = new Page7();
return tab8;
case 8:
Page8 tab9 = new Page8();
return tab9;
case 9:
Page9 tab10 = new Page9();
return tab10;
case 10:
Page10 tab11 = new Page10();
return tab11;
default:
return null;
}
}
@Override
public int getCount() {
return mNumOfTabs;
}
}
您需要使 TabLayout 可滚动。只需在您的布局 xml.
中添加 app:tabMode="scrollable"
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/toolbar"
android:background="?attr/colorPrimaryDark"
android:elevation="6dp"
android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:tabMode="scrollable"/>
在操作栏中添加过多选项卡后,选项卡标题文本变小且彼此靠近。我试图更改操作栏的宽度,但没有帮助。请帮助我,对不起我的语言不好。这是我到目前为止的编码,
MainActivity.java
package com.topitideas.dailymasnoonduain;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setText("Home"));
tabLayout.addTab(tabLayout.newTab().setText("Page 1"));
tabLayout.addTab(tabLayout.newTab().setText("Page 2"));
tabLayout.addTab(tabLayout.newTab().setText("Page 3"));
tabLayout.addTab(tabLayout.newTab().setText("Page 4"));
tabLayout.addTab(tabLayout.newTab().setText("Page 5"));
tabLayout.addTab(tabLayout.newTab().setText("Page 6"));
tabLayout.addTab(tabLayout.newTab().setText("Page 7"));
tabLayout.addTab(tabLayout.newTab().setText("Page 8"));
tabLayout.addTab(tabLayout.newTab().setText("Page 9"));
tabLayout.addTab(tabLayout.newTab().setText("Page 10"));
tabLayout.addTab(tabLayout.newTab().setText("Page 11"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
final PagerAdapter adapter = new PagerAdapter
(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
activity_main.xml
<RelativeLayout
android:id="@+id/main_layout"
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"
tools:context=".MainActivity">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="?attr/colorPrimary"
android:elevation="6dp"
android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/toolbar"
android:background="?attr/colorPrimaryDark"
android:elevation="6dp"
android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_below="@id/tab_layout"/>
</RelativeLayout>
PagerAdapter.java
package com.topitideas.dailymasnoonduain;
/**
* Created by Saeed on 11/9/2015.
*/
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
public class PagerAdapter extends FragmentStatePagerAdapter {
int mNumOfTabs;
public PagerAdapter(FragmentManager fm, int NumOfTabs) {
super(fm);
this.mNumOfTabs = NumOfTabs;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
Home tab1 = new Home();
return tab1;
case 1:
Page1 tab2 = new Page1();
return tab2;
case 2:
Page2 tab3 = new Page2();
return tab3;
case 3:
Page3 tab4 = new Page3();
return tab4;
case 4:
Page4 tab5 = new Page4();
return tab5;
case 5:
Page5 tab6 = new Page5();
return tab6;
case 6:
Page6 tab7 = new Page6();
return tab7;
case 7:
Page7 tab8 = new Page7();
return tab8;
case 8:
Page8 tab9 = new Page8();
return tab9;
case 9:
Page9 tab10 = new Page9();
return tab10;
case 10:
Page10 tab11 = new Page10();
return tab11;
default:
return null;
}
}
@Override
public int getCount() {
return mNumOfTabs;
}
}
您需要使 TabLayout 可滚动。只需在您的布局 xml.
中添加app:tabMode="scrollable"
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/toolbar"
android:background="?attr/colorPrimaryDark"
android:elevation="6dp"
android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:tabMode="scrollable"/>