Android 中不同大小的选项卡

发布于 2024-09-15 23:35:34 字数 101 浏览 3 评论 0原文

我有一个有 4 个选项卡的应用程序。默认情况下,每个选项卡宽度为屏幕宽度的 1/4。我怎样才能覆盖这个?

我需要每个选项卡具有不同的宽度。关于如何实现这一目标有什么想法吗?

I have an application with 4 tabs. By default every tab width is 1/4 of the screen width. How can I override this?

I need the tabs to have a different width for each one. Any ideas on how to accomplish that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一杆小烟枪 2024-09-22 23:35:34

试试这个:

tabHost.getTabWidget().getChildAt(0).getLayoutParams().height = 35; 

Try this:

tabHost.getTabWidget().getChildAt(0).getLayoutParams().height = 35; 
时常饿 2024-09-22 23:35:34

最后,我设法使用 ToggleButton 获得不同大小的选项卡。

我的 XML 是这样的

<LinearLayout
  android:id="@+id/tabs"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:orientation="horizontal"
  android:layout_below="@id/header"
>

  <ToggleButton
  android:id="@+id/tab1"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
    android:drawableRight="@drawable/icon_home"
    android:textOn=""
    android:textOff=""
    android:background="@drawable/tabselector"
    android:paddingLeft="15dip"
    android:paddingRight="15dip"
    android:layout_weight="0"
    />
        <ImageView
    android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:src="@drawable/top_menubar_separator" />

<ToggleButton
  android:id="@+id/tab2"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
    android:drawableRight="@drawable/icon_store"
    android:textOn="Store"
    android:textOff="Store"
    android:background="@drawable/tabselector"
    android:paddingLeft="15dip"
    android:paddingRight="15dip"
    android:layout_weight="2"
    />

,其中每个 ToggleButton 都是一个选项卡。这里的技巧是使用重量来展开选项卡以完全填满屏幕。

teab_selector.xml 看起来像这样,

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item   android:drawable="@drawable/top_menubar_selected"
            android:state_pressed="true"
           />
 <item  android:drawable="@drawable/top_menubar_selected"
            android:state_checked="true"
           />
      <item     android:drawable="@drawable/top_menubar_1px" />
</selector>

我仍然需要编写逻辑,以便在选择新按钮时取消按下按钮并使用 ta 活动启动意图,但到目前为止看起来不错。

Finally I managed to have a different sized tabs using ToggleButton.

My XML is like this

<LinearLayout
  android:id="@+id/tabs"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:orientation="horizontal"
  android:layout_below="@id/header"
>

  <ToggleButton
  android:id="@+id/tab1"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
    android:drawableRight="@drawable/icon_home"
    android:textOn=""
    android:textOff=""
    android:background="@drawable/tabselector"
    android:paddingLeft="15dip"
    android:paddingRight="15dip"
    android:layout_weight="0"
    />
        <ImageView
    android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:src="@drawable/top_menubar_separator" />

<ToggleButton
  android:id="@+id/tab2"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
    android:drawableRight="@drawable/icon_store"
    android:textOn="Store"
    android:textOff="Store"
    android:background="@drawable/tabselector"
    android:paddingLeft="15dip"
    android:paddingRight="15dip"
    android:layout_weight="2"
    />

Where each ToggleButton is a tab. The trick here is using the weight to expand the tabs to completely fill the screen.

The teab_selector.xml looks like this

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item   android:drawable="@drawable/top_menubar_selected"
            android:state_pressed="true"
           />
 <item  android:drawable="@drawable/top_menubar_selected"
            android:state_checked="true"
           />
      <item     android:drawable="@drawable/top_menubar_1px" />
</selector>

I still need to code the logic for un-press a button when a new one is selected and launch an intent with ta activity, but looks nice so far.

那片花海 2024-09-22 23:35:34

试试这个:

<TabWidget android:layout_width="wrap_content" ... />

Try this:

<TabWidget android:layout_width="wrap_content" ... />
千纸鹤带着心事 2024-09-22 23:35:34

对于仍在使用 FragmentTabHost 和 TabWidget 的人来说,可以使用 ViewTreeObserver 上的 PreDrawListener 来让选项卡换行到文本而不使用字内分隔策略。 > 选项卡指示器视图,然后在其中设置文本。像这样的东西

    final View newTabView = LayoutInflater.from(this).inflate(R.layout.tab_view, null);
    mTabHost.addTab(mTabHost.newTabSpec(index + "").setIndicator(newTabView),
            YourClassArgument.class, null);
    final TextView titleTextView = newTabView.findViewById(R.id.tabTextView);
    titleTextView.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
        @Override
        public boolean onPreDraw() {
            titleTextView.getViewTreeObserver().removeOnPreDrawListener(this);
            LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) newTabView.getLayoutParams();
            layoutParams.width = (int) Math.ceil(titleTextView.getLayout().getLineWidth(0))
                    + newTabView.getPaddingLeft() + newTabView.getPaddingRight();
            newTabView.setLayoutParams(layoutParams);
            return false;
        }
    });
    titleTextView.setText(tabTitle);

For the sake of someone still using FragmentTabHost and TabWidget, getting the tab to wrap to the text and not use a within-word break strategy can be achieved using a PreDrawListener on the ViewTreeObserver of the tab indicator view before setting the text in it. Something like this

    final View newTabView = LayoutInflater.from(this).inflate(R.layout.tab_view, null);
    mTabHost.addTab(mTabHost.newTabSpec(index + "").setIndicator(newTabView),
            YourClassArgument.class, null);
    final TextView titleTextView = newTabView.findViewById(R.id.tabTextView);
    titleTextView.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
        @Override
        public boolean onPreDraw() {
            titleTextView.getViewTreeObserver().removeOnPreDrawListener(this);
            LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) newTabView.getLayoutParams();
            layoutParams.width = (int) Math.ceil(titleTextView.getLayout().getLineWidth(0))
                    + newTabView.getPaddingLeft() + newTabView.getPaddingRight();
            newTabView.setLayoutParams(layoutParams);
            return false;
        }
    });
    titleTextView.setText(tabTitle);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文