Procházet zdrojové kódy

玩转游戏顶部文字与下划线一样长

zengjiebin před 7 roky
rodič
revize
67ffe6a4a8

+ 3 - 0
app/src/main/java/com/sheep/gamegroup/module/game/fragment/FgtGameCenter.java

@@ -7,6 +7,7 @@ import android.view.View;
 import android.widget.Button;
 import android.widget.ImageButton;
 
+import com.sheep.gamegroup.util.CommonUtil;
 import com.sheep.gamegroup.util.Jump2View;
 import com.sheep.gamegroup.util.KeyEventUtil;
 import com.sheep.gamegroup.util.ViewUtil;
@@ -15,6 +16,7 @@ import com.sheep.gamegroup.view.adapter.TitleFragmentListAdapter;
 import com.sheep.gamegroup.view.fragment.FgtGiftCenter;
 import com.sheep.jiuyan.samllsheep.R;
 import com.sheep.jiuyan.samllsheep.base.BaseFragment;
+import com.sheep.jiuyan.samllsheep.utils.G;
 
 import butterknife.BindView;
 import butterknife.OnClick;
@@ -57,6 +59,7 @@ public class FgtGameCenter extends BaseFragment {
         mAdapter.add(new FgtGiftCenter(), "礼包中心");
         pager.setAdapter(mAdapter);
         indicator.setupWithViewPager(pager);
+        CommonUtil.getInstance().reflex(indicator, getActivity(), false, G.WIDTH - G.getRealPix(144));
         pager.setOffscreenPageLimit(mAdapter.getCount());
         pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
             @Override

+ 52 - 43
app/src/main/java/com/sheep/gamegroup/util/CommonUtil.java

@@ -1704,7 +1704,7 @@ public class CommonUtil {
      * @param tabLayout
      */
     public void reflex(final TabLayout tabLayout, final Context context) {
-        reflex(tabLayout, context, false);
+        reflex(tabLayout, context, false, 0);
     }
 
     /**
@@ -1712,60 +1712,69 @@ public class CommonUtil {
      *
      * @param tabLayout
      */
-    public void reflex(final TabLayout tabLayout, final Context context, final boolean reMeasure) {
+    public void reflex(final TabLayout tabLayout, final Context context, boolean reMeasure) {
+        reflex(tabLayout, context, reMeasure, 0);
+    }
+
+    /**
+     * 通过反射调整tabLayout中下划线的宽度
+     *
+     * @param tabLayout
+     */
+    public void reflex(final TabLayout tabLayout, final Context context, final boolean reMeasure, int tw) {
         //了解源码得知 线的宽度是根据 tabView的宽度来设置的
-        tabLayout.post(new Runnable() {
-            @Override
-            public void run() {
-                try {
-                    //拿到tabLayout的mTabStrip属性
-                    LinearLayout mTabStrip = (LinearLayout) tabLayout.getChildAt(0);
+        tabLayout.post(() -> {
+            try {
+                //拿到tabLayout的mTabStrip属性
+                LinearLayout mTabStrip = (LinearLayout) tabLayout.getChildAt(0);
 
 //                    int dp10 = dip2px(tabLayout.getContext(), 10);
-                    int add = context.getResources().getDimensionPixelSize(R.dimen.content_padding_10);
-                    int count = mTabStrip.getChildCount();
-                    int fillCount = Math.min(5, count);//一屏的个数
+                int add = context.getResources().getDimensionPixelSize(R.dimen.content_padding_10);
+                int count = mTabStrip.getChildCount();
+                int fillCount = Math.min(5, count);//一屏的个数
 
-                    int tabWidth = tabLayout.getWidth();
-                    if (tabWidth == 0) {
-                        tabWidth = G.WIDTH;
-                    }
-                    for (int i = 0; i < count; i++) {
-                        View tabView = mTabStrip.getChildAt(i);
+                int tabWidth = tw;
+                if (tabWidth == 0) {
+                    tabWidth = tabLayout.getWidth();
+                }
+                if (tabWidth == 0) {
+                    tabWidth = G.WIDTH;
+                }
+                for (int i = 0; i < count; i++) {
+                    View tabView = mTabStrip.getChildAt(i);
 
-                        //拿到tabView的mTextView属性  tab的字数不固定一定用反射取mTextView
-                        Field mTextViewField = tabView.getClass().getDeclaredField("textView");//com.android.support:design:28.0.0
+                    //拿到tabView的mTextView属性  tab的字数不固定一定用反射取mTextView
+                    Field mTextViewField = tabView.getClass().getDeclaredField("textView");//com.android.support:design:28.0.0
 //                        Field mTextViewField = tabView.getClass().getDeclaredField("mTextView");//com.android.support:design:27.1.0
-                        mTextViewField.setAccessible(true);
-
-                        TextView mTextView = (TextView) mTextViewField.get(tabView);
-
-                        tabView.setPadding(0, 0, 0, 0);
+                    mTextViewField.setAccessible(true);
 
-                        //因为我想要的效果是   字多宽线就多宽,所以测量mTextView的宽度
-                        int width = 0;
-                        width = mTextView.getWidth();
-                        if (width == 0 || reMeasure) {
-                            mTextView.measure(0, 0);
-                            width = mTextView.getMeasuredWidth();
-                        }
+                    TextView mTextView = (TextView) mTextViewField.get(tabView);
 
-                        //设置tab左右间距为10dp  注意这里不能使用Padding 因为源码中线的宽度是根据 tabView的宽度来设置的
-                        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) tabView.getLayoutParams();
-                        params.width = width;
-                        int margin = (tabWidth - fillCount * width - 2 * add) / (fillCount * 2);
-                        params.leftMargin = margin + (i == 0 ? add : 0);
-                        params.rightMargin = margin + (i + 1 == count ? add : 0);
-                        tabView.setLayoutParams(params);
+                    tabView.setPadding(0, 0, 0, 0);
 
-                        tabView.invalidate();
+                    //因为我想要的效果是   字多宽线就多宽,所以测量mTextView的宽度
+                    int width = 0;
+                    width = mTextView.getWidth();
+                    if (width == 0 || reMeasure) {
+                        mTextView.measure(0, 0);
+                        width = mTextView.getMeasuredWidth();
                     }
 
-                } catch (NoSuchFieldException e) {
-                    e.printStackTrace();
-                } catch (IllegalAccessException e) {
-                    e.printStackTrace();
+                    //设置tab左右间距为10dp  注意这里不能使用Padding 因为源码中线的宽度是根据 tabView的宽度来设置的
+                    LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) tabView.getLayoutParams();
+                    params.width = width;
+                    int margin = (tabWidth - fillCount * width - 2 * add) / (fillCount * 2);
+                    params.leftMargin = margin + (i == 0 ? add : 0);
+                    params.rightMargin = margin + (i + 1 == count ? add : 0);
+                    tabView.setLayoutParams(params);
+
+                    tabView.invalidate();
                 }
+
+            } catch (NoSuchFieldException e) {
+                e.printStackTrace();
+            } catch (IllegalAccessException e) {
+                e.printStackTrace();
             }
         });