2013-03-09 84 views
0

強制性背景信息:我正在爲房地產經紀人在引用房屋和建築物時使用平板應用程序。由於家庭或公寓可以擁有任意數量的房間,因此我認爲建立一個基於標籤的解決方案是非常好的,該解決方案允許逐個房間並按需爲每個房間創建一個標籤。Android - 如何使用現有佈局按需創建選項卡?

我經歷過幾個選項卡教程,但所有的解決方案,我發現處理標籤的預定數量,並使用過時TabHost。

TabHost.TabSpec ourSpec = tabhost.newTabSpec("tag1"); 
ourSpec.setContent(new TabHost.TabContentFactory() 
{ 

    @Override 
    public View createTabContent(String tag) 
    { 
     // Put some GUI stuff here 
     return null; 
    } 
}); 

問題:我想重新使用新標籤,將現有的佈局,並以某種方式保留多少選項卡已創建至今計數。

+0

描述了更多你的問題?你是否試圖從你的程序中動態地在tabhost中添加tab tab?你試過了什麼,你遇到了什麼問題 – stinepike 2013-03-10 06:03:03

+0

嗯,是的,我試圖動態添加新標籤,而不是簡單地通過XML文件來定義它們。它們都應該包含相同的GUI元素。我可以創建新標籤,但我不知道如何告訴它使用與第一個標籤相同的佈局。 – 2013-03-11 00:38:31

回答

0

經過大量的研究,終於設法把可行的東西放在一起。

桑卡爾Ganesh的tutorial是非常有用的。

MainActivity.java:

package com.example.workingdynamictabexample; 

import android.app.TabActivity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.TabHost; 

@SuppressWarnings("deprecation") 
public class MainActivity extends TabActivity 
{ 
    private TabHost tabHost; 

    private int z = 0; 

    private static final int 
     ADD_TAB = Menu.FIRST + 11, 
     DELETE_TAB = Menu.FIRST + 12; 

    private String Test = null; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     this.tabHost = getTabHost(); 

     Intent newRoom = new Intent(); 

     newRoom.setClass(this, RoomActivity.class); 

     Test = Intent.CATEGORY_LAUNCHER; 

     Log.d("Test", Test); 

     tabHost.addTab(
       tabHost.newTabSpec("Main") 
         .setIndicator("New room") 
         .setContent(newRoom) 
         ); 
    } 

    private void addTab() 
    { 
     Intent newRoom = new Intent(); 

     newRoom.setClass(this, RoomActivity.class); 

     tabHost.addTab(
       tabHost.newTabSpec("NewRoomTab") 
         .setIndicator("New room") 
         .setContent(newRoom) 
       ); 

     Log.d("z", Integer.toString(z)); 

     ++z; 
    } 

    // "Boss, we really cannot delete one a'dem tab gubbinz, so we hides 'em." 
    private void deleteTab() 
    { 
     int position = tabHost.getCurrentTab(); 
     Log.d("Position", Integer.toString(position)); 

     Log.d("Z val in delete()", Integer.toString(z)); 

     tabHost.getCurrentTabView().setVisibility(View.GONE); 

     if (position > 0) 
     { 
      tabHost.setCurrentTab(position + 1); 

      z -= 1; 

      if (z < 0) 
      { 
       z = 0; 
      } 
     } 
     else if (position == 0) 
     { 
      tabHost.setCurrentTab(position + 1); 

      z = 0; 
     } 
     else if (position == z) 
     { 
      tabHost.setCurrentTab(z - 1); 

      Log.d("Z value in final", "lol"); 
      Log.d("Pos", Integer.toString(position)); 
      Log.d("Z pos", Integer.toString(z)); 
     }  
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) 
    { 
     menu.add(Menu.NONE, ADD_TAB, Menu.NONE, "New room") 
      .setAlphabeticShortcut('a'); 

     return (super.onCreateOptionsMenu(menu)); 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) 
    { 
     switch (item.getItemId()) 
     { 
     case ADD_TAB: 
      addTab(); 

      return (true); 

     case DELETE_TAB: 
      deleteTab(); 

      return (true); 
     } 

     return (super.onOptionsItemSelected(item)); 
    } 
} 

RoomActivity.java:

package com.example.workingdynamictabexample; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.Menu; 

public class RoomActivity extends Activity 
{ 
    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.tabhost); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) 
    { 
     return (super.onCreateOptionsMenu(menu)); 
    } 
} 

activity_main.xml中:

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

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

     <TabWidget 
      android:id="@android:id/tabs" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:tag="tabPane" /> 

      <FrameLayout 
       android:id="@android:id/tabcontent" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent">     
      </FrameLayout> 

    </LinearLayout> 
</TabHost> 

tabhost.xml:

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

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

     <TabWidget 
      android:id="@android:id/tabs" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:tag="tabPane" 
     /> 

     <FrameLayout 
      android:id="@android:id/tabcontent" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent"> 

      <RelativeLayout 
       android:id="@+id/tab_room" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent"> 

       <Button 
        android:id="@+id/btnAddTab" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_alignParentBottom="true" 
        android:layout_alignParentRight="true" 
        android:onClick="addTab" 
        android:text="Add room" /> 

       <TextView 
        android:id="@+id/lblType" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_alignParentLeft="true" 
        android:layout_alignParentTop="true" 
        android:layout_marginLeft="20dp" 
        android:layout_marginTop="30dp" 
        android:text="Room type:" /> 

       <Spinner 
        android:id="@+id/spinnerType" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_alignBottom="@+id/lblType" 
        android:layout_marginLeft="20dp" 
        android:layout_toRightOf="@+id/lblType" /> 

       <TextView 
        android:id="@+id/lblWidthX" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_alignLeft="@+id/lblType" 
        android:layout_below="@+id/lblType" 
        android:layout_marginTop="30dp" 
        android:text="Dimension 1:" /> 

       <TextView 
        android:id="@+id/lblWidthY" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_alignLeft="@+id/lblWidthX" 
        android:layout_below="@+id/lblWidthX" 
        android:layout_marginTop="30dp" 
        android:text="Dimension 2:" /> 

       <EditText 
        android:id="@+id/txtWidthX" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_alignBaseline="@+id/lblWidthX" 
        android:layout_alignBottom="@+id/lblWidthX" 
        android:layout_alignLeft="@+id/txtWidthY" 
        android:layout_alignParentRight="true" 
        android:ems="10" > 

        <requestFocus /> 
       </EditText> 

       <EditText 
        android:id="@+id/txtWidthY" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_alignBaseline="@+id/lblWidthY" 
        android:layout_alignBottom="@+id/lblWidthY" 
        android:layout_alignParentRight="true" 
        android:layout_toRightOf="@+id/lblWidthY" 
        android:ems="10" /> 

       <TextView 
        android:id="@+id/lblFloors" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_alignLeft="@+id/lblWidthY" 
        android:layout_below="@+id/txtWidthY" 
        android:layout_marginTop="30dp" 
        android:text="Floors:" /> 

       <EditText 
        android:id="@+id/txtFloors" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_alignBaseline="@+id/lblFloors" 
        android:layout_alignBottom="@+id/lblFloors" 
        android:layout_alignParentRight="true" 
        android:layout_toRightOf="@+id/lblFloors" 
        android:ems="10" /> 

      </RelativeLayout> 

     </FrameLayout> 
    </LinearLayout> 
</TabHost> 
1

爲了這個,我用下面的代碼

TabSpec fifthTabSpec = tabHost.newTabSpec("tid5"); 
addTab(fifthTabSpec , "", 
     getResources().getDrawable(R.drawable.icon), new Intent(
       A.this, B.class)); 

這裏是addTab方法

private void addTab(TabSpec spec, String labelId, Drawable drawable, 
      Intent intent) { 

     View tabIndicator = LayoutInflater.from(this).inflate(
       R.layout.tab_indicator, getTabWidget(), false); // tab_indicator is a layout for the tab widget as i used custom icon and style 

     ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon); 
     icon.setImageDrawable(drawable); 
     icon.setScaleType(ImageView.ScaleType.FIT_CENTER); 

     spec.setIndicator(tabIndicator); 
     spec.setContent(intent); 
     tabHost.addTab(spec); 
    } 

這裏是tab_indicator

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="0dip" 
android:layout_height="55dip" 
android:layout_weight="1" 
android:orientation="vertical" 
android:weightSum="55" > 

<ImageView 
    android:id="@+id/icon" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_weight="30" 
    android:adjustViewBounds="false" 
    android:padding="10dp" 
    android:background="@drawable/tab_icon_selector" // tab_icon_selector is custom selector 
    android:src="@drawable/icon" /> 

希望你會發現THI這很有幫助

+0

試過了。工程,但我看不到任何標籤按鈕了。我嘗試點擊標籤應該去的地方,並且我得到一個IllegalStateException異常,詢問我是否忘記調用setup(LocalActivityManager activityGroup)。 – 2013-03-11 21:14:59

+0

解決了上一個問題(解決方案在http://stackoverflow.com/questions/9409735/java-lang-illegalstateexception-at-tabhost-addtabspec),但我得到一個有趣的行爲:整個佈局正在複製和部分覆蓋,每次按「添加標籤」按鈕一次... – 2013-03-11 21:31:05

相關問題