繁体   English   中英

如何在Android中的选项卡旁边放置按钮? (与UI有关)

[英]How to place a button next to a Tab in android ? (Related to UI)

我想在应用程序中的“标签”旁边放置一个按钮。 我已经在下面提供了有关如何构建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>

在我的活动中的代码:

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;

}

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

APP的当前视图

我想做的就是在今天放置一个按钮。 我该怎么办?

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

感谢您的投入和时间。

在此处输入图片说明

我使用以下代码生成此结果。 我将第四个选项卡设置为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;
    }
}

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" />

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" />

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>

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>

由于TabLayout必须是布局的根节点,因此您可以尝试以下操作:创建一个具有3个TabTabLayout ,在第4个Tab的位置,您可以制作一个自己的自定义Button

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM