我正在使用TabLayout& ViewPager。 我正在尝试更改Tab的大小,如Whatsapp(相机图标)。 三个标签的大小相等,但相机的标签较小。 在每次尝试中,我都会使Tabs的大小保持不变(在所有选项卡中相等)。 感谢。
答案 0 :(得分:19)
您需要降低相应标签的LinearLayout的权重。
LinearLayout layout = ((LinearLayout) ((LinearLayout) tabLayout.getChildAt(0)).getChildAt(YOUR_TAB_NUMBER));
LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) layout.getLayoutParams();
layoutParams.weight = YOUR_WEIGHT; // e.g. 0.5f
layout.setLayoutParams(layoutParams);
希望有所帮助!
答案 1 :(得分:4)
@ digger的答案正在发挥作用。但是,如果您希望带有图标的标签仅包装其内容,则可以将权重设置为0
,将宽度设置为LinearLayout.LayoutParams.WRAP_CONTENT
。它对我有用。
fun TabLayout.setTabWidthAsWrapContent(tabPosition: Int) {
val layout = (this.getChildAt(0) as LinearLayout).getChildAt(tabPosition) as LinearLayout
val layoutParams = layout.layoutParams as LinearLayout.LayoutParams
layoutParams.weight = 0f
layoutParams.width = LinearLayout.LayoutParams.WRAP_CONTENT
layout.layoutParams = layoutParams
}
答案 2 :(得分:1)
这里我拍摄了5个标签,第1个标签宽度为屏幕宽度的12%,其余4个共享剩余宽度。在给视图充气后经过400毫秒的延迟后再进行此操作。
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager()getDefaultDisplay()getMetrics(displaymetrics);
int width= displaymetrics.widthPixels;
int widthOtherThanFirst= (int) ((float)width*(8.80f/10f));
int allOther=widthOtherThanFirst/4;
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
LinearLayout layout1 = ((LinearLayout) ((LinearLayout)tabLayout.getChildAt(0)).getChildAt(0));
LinearLayout.LayoutParams layoutParams1 = (LinearLayout.LayoutParams) layout1.getLayoutParams();
layoutParams1.width = width-widthOtherThanFirst;
layout1.setPadding(0,0,0,0);
layout1.setLayoutParams(layoutParams1);
LinearLayout layout2 = ((LinearLayout) ((LinearLayout)tabLayout.getChildAt(0)).getChildAt(1));
LinearLayout.LayoutParams layoutParams2 = (LinearLayout.LayoutParams) layout2.getLayoutParams();
layoutParams2.width = allOther;
layout2.setLayoutParams(layoutParams2);
LinearLayout layout5 = ((LinearLayout) ((LinearLayout)tabLayout.getChildAt(0)).getChildAt(2));
LinearLayout.LayoutParams layoutParams5 = (LinearLayout.LayoutParams) layout5.getLayoutParams();
layoutParams5.width = allOther;
layout5.setLayoutParams(layoutParams5);
LinearLayout layout3 = ((LinearLayout) ((LinearLayout)tabLayout.getChildAt(0)).getChildAt(3));
LinearLayout.LayoutParams layoutParams3 = (LinearLayout.LayoutParams) layout3.getLayoutParams();
layoutParams3.width = allOther;
layout3.setLayoutParams(layoutParams3);
LinearLayout layout4 = ((LinearLayout) ((LinearLayout)tabLayout.getChildAt(0)).getChildAt(4));
LinearLayout.LayoutParams layoutParams4 = (LinearLayout.LayoutParams) layout4.getLayoutParams();
layoutParams4.width = allOther;
layout4.setLayoutParams(layoutParams4);
tabLayout.invalidate();
}
},400);
答案 3 :(得分:0)
用 wrap_content 背景检查动态宽度
private fun setTabwidth(tabposition: Int, weight: Float) {
val layout: LinearLayout = tabLayout?.getChildAt(0) as LinearLayout).getChildAt(tabpos) as LinearLayout
val layoutParams: LinearLayout.LayoutParams = layout.getLayoutParams() as LinearLayout.LayoutParams
layoutParams.weight = weight
layoutParams.width = LinearLayout.LayoutParams.WRAP_CONTENT
layout.setLayoutParams(layoutParams)
val tablayoutParams: ViewGroup.LayoutParams? = tabLayout?.layoutParams
tablayoutParams?.width=ViewGroup.LayoutParams.WRAP_CONTENT
tabLayout?.layoutParams=tablayoutParams
}
用法:
setTabwidth(0,0.4f) // first tab 0.4 weight
setTabwidth(1,0.6f) // second tab 0.6 weight