【问题标题】:Programmatically add tabs to tab host以编程方式将选项卡添加到选项卡主机
【发布时间】:2014-12-17 16:51:51
【问题描述】:

我想知道以编程方式将选项卡添加到选项卡主机的最简单方法(例如,通过按下按钮)。换句话说,无需在 XML 文件中对其结构进行硬编码。

【问题讨论】:

标签: android xml tabs add


【解决方案1】:

以编程方式创建选项卡并为其填充内容的一种方法是:

  1. 定义单独的布局文件
  2. 定义一个 Tab Content Factory 类,该类会扩展布局并在处理完基本自定义后返回结果视图
  3. 使用 Factory 类作为 TabSpec.setContent 方法的参数

示例

单独的 Layout.axml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:minWidth="25px"
    android:minHeight="25px">
    <TextView
        android:text="Kablam"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/kablamTextView" />
</LinearLayout>

TabContent Factory 类(定义为Activity 中的内部类 => this 是活动实例)

private class MyTabContentFactory implements TabContentFactory {
   public View CreateTabContent(string tag) {

      View view = this.getLayoutInflater()
             .inflate(R.layout.Layout, (ViewGroup)this.FindViewById(R.id.tabHost1), false);

      ((TextView)view.findViewById(R.id.kablamTextView))
         .setText("Some sentence which can be generated dynamically");
      return view;
    }
}

最后以Hemendra Sharma 的回答为基础,使用标签工厂来定义内容

TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost);
tabHost.setup();

TabSpec tab1 = tabHost.newTabSpec("Tab_Name");
tab1.setIndicator("Tab 1");

tab1.setContent(new MyTabContentFactory());

tabHost.addTab(tab1);

【讨论】:

    【解决方案2】:

    这是一种以编程方式在 TabHost 上添加选项卡的简单方法。

    TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost);
    tabHost.setup();
    
    TabSpec tab1 = tabHost.newTabSpec("Tab_Name");
    View view = getLayoutInflater().inflate(R.layout.tab_indicator,
            myLayout, false);
    tab1.setIndicator(view);
    Intent i = new Intent(getApplicationContext(), MyActivity.class);
    tab1.setContent(i);
    
    tabHost.addTab(tab1);
    

    祝你好运。 :)

    【讨论】:

    • 首先非常感谢您的帮助。问题是我完全是一个新手,试图为我做一些困难的事情。所以问题是: View view = getLayoutInflater().inflate(R.layout.tab_indicator,myLayout, false);如何设置 mylayout 和 tab_indicator?它是动态的,所以我不能将它存储在 activity_main.xml 中可以吗?
    • 为您的指标创建一个新的 XML 布局文件。它不会在 activity_main.xml 中。
    • 再次感谢@Hemendra。所以我在布局目录中创建了 tab_layout.xml。这个布局有一行 android:id="@+id/LinearLayout01"。我的最后一个问题是:在行 View view = getLayoutInflater().inflate(R.layout.tab_indicator, myLayout, false); myLayout var 设置为什么?再次感谢并为我的笨拙感到抱歉...
    【解决方案3】:

    在按钮点击事件上,试试这个

    myTabHost =(TabHost) findViewById(R.id.tabhostId);
    mytabhost.setup();
    TabSpec spec = mytabhost.newTabSpec("tab_creation");
     spec.setIndicator("TAB_NAME",getResources().getDrawable(android.R.drawable.ic_menu_add));// text and image of tab
    spec.setContent(R.id.layout_of_tab); // layout of tab
    mytabhost.addTab(spec);
    

    【讨论】:

      猜你喜欢
      • 2016-12-30
      • 1970-01-01
      • 1970-01-01
      • 2017-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-12
      相关资源
      最近更新 更多