如何在android中的Tab旁边放置一个按钮?(与UI相关)

Vin*_*oth 1 java user-interface android button

我想在我的应用程序中的选项卡旁边放置一个按钮.我已经给出了下面的代码,我将如何构建我的Tabs.

这是XML

<TabHost
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="330dp"
     >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <View
            android:layout_width="fill_parent"
            android:layout_height="0.5dip"
            android:background="#000" />

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="0dip"
            android:layout_marginRight="0dip" />

        <View
            android:layout_width="fill_parent"
            android:layout_height="2dip"
            android:background="#696969" />

        <View
            android:layout_width="fill_parent"
            android:layout_height="2dip"
            android:background="#000" />

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />
    </LinearLayout>
</TabHost>
Run Code Online (Sandbox Code Playgroud)

我的活动代码:

public class Secondactivity extends TabActivity {
protected void onCreate(Bundle savedInstanceState) {
    setupTabHost();
          mTabHost.getTabWidget().setDividerDrawable(se.copernicus.activity.R.drawable.tab_divider);

    setupTab(new TextView(this), "Month");
    setupTab(new TextView(this), "Week");
    setupTab(new TextView(this), "Day");
    setupTab(new TextView(this), "Today");


private void setupTab(final View view, final String tag) {
View tabview = createTabView(mTabHost.getContext(), tag);

TabSpec setContent = mTabHost.newTabSpec(tag).setIndicator(tabview)
        .setContent(new TabContentFactory() {
            public View createTabContent(String tag) {
                return view;
            }
        });
mTabHost.addTab(setContent);

}

private static View createTabView(final Context context, final String text) {
View view = LayoutInflater.from(context)
        .inflate(R.layout.tabs_bg, null);
TextView tv = (TextView) view.findViewById(R.id.tabsText);
tv.setText(text);
return view;
Run Code Online (Sandbox Code Playgroud)

}

这是我运行应用程序时的当前视图

APP的当前观点

我想做的是在今天的地方放一个按钮.我该怎么办呢?

我已经排除了一些xml代码,它将显示压缩状态,因为问题会变得冗长

谢谢你输入和你的时间.

Kan*_*esh 6

在此输入图像描述

我使用以下代码生成此结果.我已将第四个标签设置为clickable false.只有选项卡内的按钮才可以点击.因此,它会给人一种印象,即它不是标签.希望这会有所帮助.

活动:

public class MainActivity extends TabActivity {
    private TabHost mTabHost;

    private void setupTabHost() {
    mTabHost = (TabHost) findViewById(android.R.id.tabhost);
    mTabHost.setup();
    }

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.home);

    setupTabHost();
    mTabHost.getTabWidget().setDividerDrawable(R.drawable.item_seperator);

    setupTab(new TextView(this), "Month");
    setupTab(new TextView(this), "Week");
    setupTab(new TextView(this), "Day");

    final View view = new Button(this);

    View tabview = LayoutInflater.from(this).inflate(
        R.layout.button_tabs_bg, null);
    Button button = (Button) tabview.findViewById(R.id.tabsButton);
    button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
        Toast.makeText(MainActivity.this, "Button click",
            Toast.LENGTH_SHORT).show();
        }
    });

    TabSpec setContent = mTabHost.newTabSpec("").setIndicator(tabview)
        .setContent(new TabContentFactory() {
            public View createTabContent(String tag) {

            return view;
            }
        });

    mTabHost.addTab(setContent);

    mTabHost.getTabWidget().getChildTabViewAt(3).setEnabled(false);

    }

    private void setupTab(final View view, final String tag) {
    View tabview = createTabView(mTabHost.getContext(), tag);

    TabSpec setContent = mTabHost.newTabSpec(tag).setIndicator(tabview)
        .setContent(new TabContentFactory() {
            public View createTabContent(String tag) {
            return view;
            }
        });

    mTabHost.addTab(setContent);
    }

    private static View createTabView(final Context context, final String text) {
    View view = LayoutInflater.from(context)
        .inflate(R.layout.tabs_bg, null);
    TextView tv = (TextView) view.findViewById(R.id.tabsText);
    tv.setText(text);
    return view;
    }
}
Run Code Online (Sandbox Code Playgroud)

tab_bg_selector.xml

<!-- Active tab -->
<item android:state_selected="true" android:state_focused="false"
    android:state_pressed="false" android:drawable="@color/tablight" />
<!-- Inactive tab -->
<item android:state_selected="false" android:state_focused="false"
    android:state_pressed="false" android:drawable="@color/tabdark" />
<!-- Pressed tab -->
<item android:state_pressed="true" android:drawable="@color/tablight" />
<!-- Selected tab (using d-pad) -->
<item android:state_focused="true" android:state_selected="true"
    android:state_pressed="false" android:drawable="@color/tablight" />
Run Code Online (Sandbox Code Playgroud)

tabs_bg.xml

<TextView
    android:id="@+id/tabsText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Title"
    android:textColor="@android:color/white"
    android:textSize="15dip" />
Run Code Online (Sandbox Code Playgroud)

button_tabs_bg.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tabsLayout" android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:background="@color/buttontab"
    android:padding="10dip" android:gravity="center" android:orientation="vertical">

    <Button
        android:id="@+id/tabsButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        android:textColor="@android:color/black"
        android:textSize="15dip" />

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="tabdark">#333333</color>
    <color name="tablight">#999999</color>
    <color name="buttontab">#999966</color>
</resources>
Run Code Online (Sandbox Code Playgroud)