Modify TableLayout's underline indicator length

/**
 * Modify the underline length of TableLayout through reflection mechanism
 */

public void setIndicator(TabLayout tabs, int leftDip, int rightDip) {
    // get it by reflection
    Class tabLayout = tabs.getClass();
    Field tabStrip = null;
    try {
        tabStrip = tabLayout.getDeclaredField("mTabStrip");
    } catch (NoSuchFieldException e) {
        e.printStackTrace ();
    }
    //set mode
    tabStrip.setAccessible(true);
    //get tabview
    LinearLayout llTab = null;
    try {
        llTab = (LinearLayout) tabStrip.get(tabs);
    } catch (IllegalAccessException e) {
        e.printStackTrace ();
    }
    //Set the padding of the tabView to 0, and set the margin
    int left = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, leftDip, Resources.getSystem().getDisplayMetrics());
    int right = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, rightDip, Resources.getSystem().getDisplayMetrics());
    for (int i = 0; i < llTab.getChildCount(); i++) {
        View child = llTab.getChildAt(i);
        child.setPadding(0, 0, 0, 0);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1);
        params.leftMargin = left;
        params.rightMargin = right;
        child.setLayoutParams(params);
        child.invalidate();
    }
}

transfer

//Set the underscore length
homeFragment_tab.post(new Runnable() {
    @Override
    public void run() {
        setIndicator(homeFragment_tab,60,60);
    }
});

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325171648&siteId=291194637