簡體   English   中英

如何在 Android 中的選項卡按鈕之間添加空格?

[英]How to add space between tab buttons in Android?

我需要用空格分隔選項卡按鈕,我嘗試為視圖設置邊距,然后將它們添加為選項卡,但它不起作用,我也想過添加空視圖作為分隔符,但還沒有嘗試過,有沒有這樣做的標准方式,或任何可以達到相同效果的調整?

謝謝!

方法如下:

TabWidget tabWidget = (TabWidget) findViewById(android.R.id.tabs);
final int tabChildrenCount = tabWidget.getChildCount();
View currentView;
for (int i = 0; i < tabChildrenCount; i++) {
    currentView = tabWidget.getChildAt(i);
    LinearLayout.LayoutParams currentLayout =
        (LinearLayout.LayoutParams) currentView.getLayoutParams();
    currentLayout.setMargins(0, 5, 5, 0);
}
tabWidget.requestLayout();

即使對於我的問題,這確實是一個很好的解決方案,非常感謝。 我用它在小部件中的第一個和最后一個項目之前和之后實現空間,以便可以將它們滾動到中心可見,而無需添加額外的(並且令人不安,因為小部件不期望這種愚蠢的東西)不可見按鈕。

    //pump up space for first entry on the left and last entry on the right!
    Display display = getWindowManager().getDefaultDisplay();
    //Point size = new Point();
    int width = display.getWidth();

    View currentView = mTabHost.getTabWidget().getChildAt(0);
    LinearLayout.LayoutParams currentLayout = (LinearLayout.LayoutParams) currentView.getLayoutParams();
    currentLayout.setMargins(currentLayout.leftMargin + width/2, currentLayout.topMargin, currentLayout.rightMargin, currentLayout.bottomMargin);
    currentView = mTabHost.getTabWidget().getChildAt(mTabHost.getTabWidget().getChildCount()-1);
    currentLayout = (LinearLayout.LayoutParams) currentView.getLayoutParams();
    currentLayout.setMargins(currentLayout.leftMargin, currentLayout.topMargin, currentLayout.rightMargin  + width/2, currentLayout.bottomMargin);      
    mTabHost.getTabWidget().requestLayout();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM