how to change swipe tabs text size for different screen size? I am using fragments and view pager to create swipe tabs

旧时模样 提交于 2019-12-12 04:56:25

问题


here is my java code where i set tabs and its text

 ublic class MainActivity extends ActionBarActivity implements TabListener {

private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
//Tab Tiles
private String [] tabs;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    //initilization 
    viewPager =(ViewPager)findViewById(R.id.pager);
    actionBar = getSupportActionBar();
    mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
    viewPager.setAdapter(mAdapter);
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
     tabs = getResources().getStringArray(R.array.tabs);
    //Hiding Title bar
    actionBar.setDisplayShowHomeEnabled(false);
    actionBar.setDisplayShowTitleEnabled(false);
    actionBar.setDisplayUseLogoEnabled(false);

    //Adding tabs 

    for (String tab_name : tabs) {
        actionBar.addTab(actionBar.newTab().setText(tab_name).setTabListener(this));
    }//for each

and my manifest file is

       <activity
        android:name="com.galleryview.MainActivity"
        android:label="@string/app_name" 
        android:theme="@style/Theme.AppCompat.Light">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

The text size of tabs is good for normal phones but on tablet it looks too small..any idea how to do it in easy way ? Thanks in advance


回答1:


You can use setCustomView method and create a custom layout with different text sizes from dimens.xml file using configuration qualifiers

MainActivity.java

LayoutInflater inflater = (LayoutInflater) this.getSystemService
  (Context.LAYOUT_INFLATER_SERVICE);
TextView tv = (TextView) inflater.inflate(R.layout.customTextView, null);
tv.setText(tab_name);
actionBar.addTab(actionBar.newTab().setCustomView(tv).setTabListener(this));

customTextView.xml on layout folder

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="@dimen/actionbar_tab_text_size" />

dimens.xml on values folder

<resources>
    <dimen name="actionbar_tab_text_size">16sp</dimen>
</resources>

dimens.xml on values-large folder

<resources>
    <dimen name="actionbar_tab_text_size">20sp</dimen>
</resources>


来源:https://stackoverflow.com/questions/24822492/how-to-change-swipe-tabs-text-size-for-different-screen-size-i-am-using-fragmen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!