2016-03-04 80 views
1

我tablayout:TabLayout模式滾動不適合以及針對平板電腦

<android.support.design.widget.TabLayout 
    android:id="@+id/tab_layout" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    app:tabMode="scrollable"/> 

這裏是手機中的結果: enter image description here

和平板電腦上: enter image description here

enter image description here

你可以看到我有3個標籤:at,after和recurrence。在手機上,標籤寬度不一樣,因此它們可以非常適合DialogFragment。但在平板電腦上,它們的寬度變得相同,即使這三個字的長度也是非常不同的。我的SDK版本是Android 4.1(手機)和4.3(平板電腦,我也在4.4和同樣的問題上測試它)。我怎樣才能解決這個問題?

回答

1

使用它可能會有所幫助。我可以像這樣顯示Tab。

public class CustomTabLayout extends TabLayout { 
    public CustomTabLayout(Context context) { 
     super(context); 
    } 

    public CustomTabLayout(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public CustomTabLayout(Context context, AttributeSet attrs, int defStyleAttr) { 
     super(context, attrs, defStyleAttr); 
    } 

    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
     try { 
      if (getTabCount() == 0) 
       return; 
      Field field = TabLayout.class.getDeclaredField("mTabMinWidth"); 
      field.setAccessible(true); 
      field.set(this, (int) (getMeasuredWidth()/(float) getTabCount())); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
} 
+1

我明白了。現在,我在'layout.xml'中爲'TabLayout'添加'app:tabMinWidth =「56dp」',它變得很好。謝謝。 – ywwynm

+0

如果它幫助你然後標記它接受答案? – 2016-03-04 04:43:49