Android 如何降低标签栏高度并显示在底部

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3683363/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:20:29  来源:igfitidea点击:

How to reduce the tab bar height and display in bottom

android

提问by mohan

can anybody tell how to reduce the height of tab bar and display tab bar in bottom

任何人都可以告诉如何降低标签栏的高度并在底部显示标签栏

Thanks

谢谢

回答by Richipal

use the following line of code to change the height, this is the last line in my onCreate method

使用以下代码行更改高度,这是我的 onCreate 方法中的最后一行

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

回答by Amir Sagri

Add the following method to your class that extends TabActivity

将以下方法添加到扩展 TabActivity 的类中

 public void addNewTab(Context context, String title, int height){
     TabHost tabHost = getTabHost();  // The activity TabHost
     Intent intent = new Intent().setClass(context, HelloTabsActivity.class);
     TabHost.TabSpec spec = tabHost.newTabSpec(title.toLowerCase()).setIndicator(title).setContent(intent);
     tabHost.addTab(spec);
     int totalTabs = tabHost.getTabWidget().getChildCount();
     ((RelativeLayout)tabHost.getTabWidget().getChildTabViewAt(totalTabs-1)).removeViewAt(0);
     ((TextView)((RelativeLayout)tabHost.getTabWidget().getChildTabViewAt(totalTabs-1)).getChildAt(0)).setHeight(30);
     tabHost.getTabWidget().getChildAt(totalTabs-1).getLayoutParams().height = height;
 }

then call it like this addNewTab(this, "tab title", 30);

然后像这样调用它 addNewTab(this, "tab title", 30);

回答by 18446744073709551615

Note that you have to change the height of eachtab. For two tabs:

请注意,您必须更改每个选项卡的高度。对于两个选项卡:

@Override
public void onCreate(Bundle savedInstanceState) {
    ... // other code
    final int height = 45;
    mTabHost.getTabWidget().getChildAt(0).getLayoutParams().height = height;
    mTabHost.getTabWidget().getChildAt(1).getLayoutParams().height = height;
}

回答by DonGru

You could either

你也可以

  • Build your own tab using a TableLayoutat the bottom of the screen - which gives you quite a lot of flexibility
  • 使用TableLayout屏幕底部的构建您自己的选项卡- 这为您提供了很大的灵活性

or

或者

  • Modify use the existing TabHost/TabWidget- works in principle but I don't know how to reduce the tab bar height. Works like that:
  • 修改使用现有的TabHost/ TabWidget- 原则上有效,但我不知道如何降低标签栏高度。像这样工作:

Layout file main.xml:

布局文件main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TabHost android:id="@+id/tab_host"
        android:layout_width="fill_parent" android:layout_height="fill_parent">
        <TabWidget android:layout_width="fill_parent"
            android:layout_height="wrap_content" android:id="@android:id/tabs"
            android:layout_gravity="bottom" />
        <FrameLayout android:id="@android:id/tabcontent"
            android:layout_width="fill_parent" android:layout_height="fill_parent" >
            <LinearLayout android:id="@+id/first_tab"
                android:orientation="vertical" android:layout_width="fill_parent"
                android:layout_height="fill_parent" >
                <TextView android:layout_width="fill_parent"
                    android:layout_height="fill_parent" android:text="First Tab" />
                <!-- Replace TextView with your layout content for this tab -->
            </LinearLayout>
            <LinearLayout android:id="@+id/second_tab"
                android:orientation="vertical" android:layout_width="fill_parent"
                android:layout_height="fill_parent" >
                <TextView android:layout_width="fill_parent"
                    android:layout_height="fill_parent" android:text="Second Tab" />
                <!-- Replace TextView with your layout content for this tab -->
            </LinearLayout>
            <LinearLayout android:id="@+id/third_tab"
                android:orientation="vertical" android:layout_width="fill_parent"
                android:layout_height="fill_parent" >
                <TextView android:layout_width="fill_parent"
                    android:layout_height="fill_parent" android:text="One More Tab" />
                <!-- Replace TextView with your layout content for this tab -->
            </LinearLayout>
            <LinearLayout android:id="@+id/edit_item_text_tab"
                android:orientation="vertical" android:layout_width="fill_parent"
                android:layout_height="fill_parent" />
        </FrameLayout>
    </TabHost>
</LinearLayout>

Source code of your activity, in this case StartActivity.java

在这种情况下,您的活动的源代码 StartActivity.java

import android.app.Activity;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

public class StartActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        TabHost tab_host = (TabHost) findViewById(R.id.tab_host);
        tab_host.setup();

        TabSpec tabspec1 = tab_host.newTabSpec("TAB_1");
        tabspec1.setIndicator("Tab 1");
        tabspec1.setContent(R.id.first_tab);
        tab_host.addTab(tabspec1);

        TabSpec tabspec2 = tab_host.newTabSpec("TAB_2");
        tabspec2.setIndicator("Tab 2");
        tabspec2.setContent(R.id.second_tab);
        tab_host.addTab(tabspec2);

        TabSpec tabspec3 = tab_host.newTabSpec("TAB_3");
        tabspec3.setIndicator("Tab 3");
        tabspec3.setContent(R.id.third_tab);
        tab_host.addTab(tabspec3);

        tab_host.setCurrentTab(0);
    }
}

Turns out to look like:

结果看起来像:

alt text

替代文字

回答by Jitendra Patidar

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tabhost" android:layout_width="fill_parent"
    android:layout_height="wrap_content" android:padding="1dp">

    <RelativeLayout android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <TabWidget android:id="@android:id/tabs"
            android:layout_width="fill_parent" android:layout_height="60dp"
            android:layout_alignParentBottom="true" />

        <FrameLayout android:id="@android:id/tabcontent"
            android:layout_width="fill_parent" android:layout_height="fill_parent"
            android:padding="1dp">
        </FrameLayout>
    </RelativeLayout>

回答by Konstantin Burov

Most likely you'll have to implement tabs by your own. As far as I know that's impossible with regular android tabs.

您很可能必须自己实现选项卡。据我所知,使用常规的 android 标签是不可能的。

回答by vks2005

Working with tabs in the Eclipse "graphical layout" mode is not quite intuitive.

在 Eclipse“图形布局”模式下使用选项卡不是很直观。

NOTE - Currently I am not able to attach pictures to this answer as my reputation is below 10. Maybe in the future I will edit it and add them.

注意 - 目前我无法在此答案中附加图片,因为我的声誉低于 10。也许将来我会编辑它并添加它们。

1) To change tab bar height:- Select "tabs (TabWidget)" in the Outline view. A selection/resize box should appear around the tab bar and its height can now be modified. I have made it thinner in picture1.

1) 要更改标签栏高度:- 在大纲视图中选择“标签 (TabWidget)”。选项卡栏周围应出现一个选择/调整大小框,现在可以修改其高度。我在图 1 中让它变薄了。

2) To move the tab bar to bottom:- Select "tabs (TabWidget)" in the Outline view and drop it into the LinearLayout above it (as marked in the picture 1). This will send the "tabs (TabWidget)" to the bottom of the list (see picture2). Tabs Bar might disappear at this stage. So adjust the weights of "tabcontent" & "tabs (TabWidget)" till it looks ok. I have used 10 & 1 respectively.

2) 将标签栏移到底部:- 在 Outline 视图中选择“tabs (TabWidget)”并将其放到它上面的 LinearLayout 中(如图 1 所示)。这会将“选项卡(TabWidget)”发送到列表底部(见图2)。标签栏可能会在这个阶段消失。所以调整“tabcontent”和“tabs (TabWidget)”的权重,直到它看起来没问题。我分别使用了 10 和 1。

3) To bring tab2 to the top:- After design/layout of tab1 is complete, we want to work on the next tab. But Eclipse does not allow selecting it. To bring tab2 to the top select tab1 and drop it into tabcontent. This will send tab1 to the bottom of the list and tab2 to the top. Later when work on tab2 is complete, tab3 can be brought up.

3) 将 tab2 带到顶部:- 在 tab1 的设计/布局完成后,我们要处理下一个选项卡。但是 Eclipse 不允许选择它。要将 tab2 置于顶部,请选择 tab1 并将其放入 tabcontent。这会将 tab1 发送到列表底部,将 tab2 发送到顶部。稍后当 tab2 上的工作完成时,tab3 可以被调出。

This pretty roundabout way of working in graphical layout mode but maybe better than typing xml code.

这种在图形布局模式下工作的非常迂回的方式,但可能比键入 xml 代码更好。

Cheers

干杯

回答by Bob Keathley

<TabWidget android:id="@android:id/tabs" android:tabStripEnabled="false" android:padding="0dip" android:layout_width="wrap_content" android:layout_height="fill_parent"/>

<TabWidget android:id="@android:id/tabs" android:tabStripEnabled="false" android:padding="0dip" android:layout_width="wrap_content" android:layout_height="fill_parent"/>