Android中修改TabLayout底部导航条Indicator长短的⽅
法
前⾔
对于Tablayout相信⼤家都不陌⽣,在开发中使⽤的应该很频繁了,但是底部导航条长短是固定死的,需要⾃⼰来改动长短,了半天没着⽅法,看了下官⽅建议,可以通过映射来修改⾃⼰想要的长短,其实也就⼏⾏代码的问题。
看代码:
public static void setIndicator(Context context, TabLayout tabs, int leftDip, int rightDip) {
Class<?> tabLayout = Class();
Field tabStrip = null;
try {
tabStrip = DeclaredField("mTabStrip");
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
tabStrip.setAccessible(true);
LinearLayout ll_tab = null;
try {
ll_tab = (LinearLayout) (tabs);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
int left = (int) (getDisplayMetrics(context).density * leftDip);
int right = (int) (getDisplayMetrics(context).density * rightDip);
for (int i = 0; i < ChildCount(); i++) {
View child = ChildAt(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();
}
}
leftDip和rightDip是左右间距,根据⾃⼰的需要来设置,直接调⽤即可。
很多同学都遇到问题,这⾥附上源码,⼤家可以看看:
freshview;
import android.app.Activity;
t.Context;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import flect.Field;
public class MainActivity extends AppCompatActivity {
private static int TAB_MARGIN_DIP = 11;
TabLayout tableLayout;
ViewPager viewPager;
RankingPagerAdapter adapter;
String [] strings = {"皇帝","丞相","尚书","太监"};
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
tableLayout = (TabLayout) findViewById(R.id.tabs_ranking);
viewPager = (ViewPager) findViewById(ainer_ranking);
adapter = new RankingPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(adapter);
tableLayout.setupWithViewPager(viewPager);
tableLayout.setTabTextColors(getResources().lor.black), getResources().d_pre));
tableLayout.setTabsFromPagerAdapter(adapter);
setIndicator(this, tableLayout, TAB_MARGIN_DIP, TAB_MARGIN_DIP);
}
private class RankingPagerAdapter extends FragmentStatePagerAdapter {
public RankingPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return new FuelFragment().newInstance(position, strings[position]);
}
@Override
public int getCount() {
return strings.length;
}
@Override
public CharSequence getPageTitle(int position) {
return strings[position];
}
}
public static void setIndicator(Context context, TabLayout tabs, int leftDip, int rightDip) {
Class<?> tabLayout = Class();
Field tabStrip = null;
try {
tabStrip = DeclaredField("mTabStrip");
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
tabStrip.setAccessible(true);
LinearLayout ll_tab = null;
try {
ll_tab = (LinearLayout) (tabs);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
int left = (int) (getDisplayMetrics(context).density * leftDip);
int right = (int) (getDisplayMetrics(context).density * rightDip);
for (int i = 0; i < ChildCount(); i++) {
View child = ChildAt(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();
}
}
public static DisplayMetrics getDisplayMetrics(Context context) {
DisplayMetrics metric = new DisplayMetrics();
((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(metric);
return metric;
}
public static float getPXfromDP(float value, Context context) {
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, value,
}
}
Fragment:
freshview;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
/
**
* Created by dong.he on 2016/12/9.
*/
public class FuelFragment extends Fragment {
public static String TAB_POSITION;
public static String TAB_NAME;
private String tabId;
private int tabPosition;
public FuelFragment() {
}
public static Fragment newInstance(int tabPosition, String tabName) {
FuelFragment fragment = new FuelFragment();
Bundle bundle = new Bundle();
bundle.putInt(TAB_POSITION, tabPosition);
bundle.putString(TAB_NAME, tabName);
fragment.setArguments(bundle);
导航页源码return fragment;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { return inflater.inflate(R.layout.layout, container, false);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
tabPosition = getArguments().getInt(TAB_POSITION, 0);
tabId = getArguments().getString(TAB_NAME);
((TextView) getView().findViewById()).setText(tabId);
}
}
布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="schemas.android/apk/res/android"
xmlns:app="schemas.android/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.design.widget.TabLayout
android:id="@+id/tabs_ranking"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabGravity="fill"
app:tabIndicatorColor="@color/red_pre"
app:tabMode="scrollable" />
<android.support.v4.view.ViewPager
android:id="@+id/container_ranking"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
总结
以上就是这篇⽂章的全部内容了,希望本⽂的内容对⼤家的学习或者⼯作能带来⼀定的帮助,如果有疑问⼤家可以留⾔交流,谢谢⼤家对的⽀持。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论