简体   繁体   中英

How to set the tablayout icon size for tablets

How to set the tab icon size for tablets I did a lot of research, tried a lot of methods, but couldn't adapt it to my layout. The phone screens are normal but the "7" "10" icon sizes on the tablet screen remain very small.

public class MainActivity extends AppCompatActivity  {

private TabLayout tabLayout;
private ViewPager viewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    viewPager = (ViewPager) findViewById(R.id.viewpager);
    setupViewPager(viewPager);

    tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(viewPager);
    setupTabIcons();

I get icons programmatically.

}

private void setupTabIcons() {

    TextView tabOne = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
    tabOne.setText(R.string.Scanner);
    tabOne.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_image, 0, 0);
    tabLayout.getTabAt(0).setCustomView(tabOne);


    TextView tabTwo = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
    tabTwo.setText(R.string.Create);
    tabTwo.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.create, 0, 0);
    tabLayout.getTabAt(1).setCustomView(tabTwo);

    TextView tabThree = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
    tabThree.setText(R.string.History);
    tabThree.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_history, 0, 0);
    tabLayout.getTabAt(2).setCustomView(tabThree);

    TextView tabFour = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
    tabFour.setText(R.string.Settings);
    tabFour.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_settings, 0, 0);
    tabLayout.getTabAt(3).setCustomView(tabFour);

}

}

my activity_main layout

 <com.google.android.material.appbar.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">


    <com.google.android.material.tabs.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="@dimen/_45sdp"
        app:tabGravity="fill"
        app:tabMode="fixed" />

</com.google.android.material.appbar.AppBarLayout>

custom_tab layout

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif"
android:gravity="center_horizontal"
android:textColor="@color/custom_tabtext1"
android:textSize="@dimen/_9ssp"
/>

I think you can support tablet using 3 way.

First way to create 3 layout file for tablet supports :-

res/layout/main_activity.xml           # For handsets (smaller than 600dp)
res/layout-sw600dp/main_activity.xml   # For 7” tablets (600dp wide and bigger)
res/layout-sw720dp/main_activity.xml   # For 10” tablets (720dp wide and bigger)

Second way to create 3 dimension file for tablet supports :-

values/dimens.xml              # For handsets (smaller than 600dp)
values-sw600dp/dimens.xml      # For 7” tablets (600dp wide and bigger)
values-sw720dp/dimens.xml      # For 10” tablets (720dp wide and bigger)

Third way to using "Sdp" library for tablet supports :-

https://github.com/intuit/sdp

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM