大家好我爲我的項目使用自定義選項卡。我的代碼在這裏http://pastie.org/8495005和我的main.xml在這裏http://pastie.org/8495007我的自定義選項卡布局在這裏http://pastie.org/8495010 我可以在單個選項卡中顯示圖像和文本。但我無法按照意願將一項活動轉移到另一項活動。我有四個選項卡和四個子類。我怎樣才能做到這一點?任何機構都可以幫助我做到這一點?提前致謝。如何在Android中使用自定義選項卡中的意圖?
1
A
回答
0
我在接下來的活動中犯了一些錯誤。我動態創建了我的內容。我沒有創建佈局,並將其設置爲setContentView(R.layout.train_layout);.現在它對我來說工作得很好,也很好。就這樣做。
CustomTabActivity.java
package com.joshclemm.android.tabs;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;
import android.widget.Toast;
public class CustomTabActivity extends TabActivity
{
private TabHost mTabHost;
Intent intent1,intent2,intent3,intent4;
TabHost.TabSpec spec1,spec2,spec3,spec4;
private void setupTabHost() {
mTabHost = (TabHost) findViewById(android.R.id.tabhost);
mTabHost.setup();
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setupTabHost();
Resources ressources = getResources();
setTabs() ;
mTabHost.getTabWidget().getChildTabViewAt(0).setBackgroundDrawable(null);
mTabHost.getTabWidget().getChildTabViewAt(1).setBackgroundDrawable(null);
mTabHost.getTabWidget().getChildTabViewAt(2).setBackgroundDrawable(null);
mTabHost.getTabWidget().getChildTabViewAt(3).setBackgroundDrawable(null);
}
private void setTabs()
{
addTab("Tab1",R.drawable.tab1_selector,Activity1.class);
addTab("Tab2",R.drawable.tab2_selector,Activity2.class);
addTab("Tab3",R.drawable.tab3_selector,Activity3.class);
addTab("Tab4",R.drawable.tab4_selector,Activity4.class);
}
private void addTab(String labelId, int drawableId, Class<?> c)
{
View view = LayoutInflater.from(getBaseContext()).inflate(R.layout.tabs_item, null);
TextView tv = (TextView) view.findViewById(R.id.tabsText);
ImageView img1 = (ImageView)view.findViewById(R.id.tabsImg);
tv.setText(labelId);
img1.setImageResource(drawableId);
TabHost tabHost = getTabHost();
Intent intent = new Intent(this, c);
TabHost.TabSpec spec = tabHost.newTabSpec("" + labelId);
spec.setIndicator(view);
spec.setContent(intent);
tabHost.addTab(spec);
int i = tabHost.getCurrentTab();
Toast.makeText(CustomTabActivity.this, "tab->" + i,Toast.LENGTH_SHORT).show();
if(i==0)
{
Intent intentAndroid = new Intent().setClass(this, Activity1.class);
TabSpec tabSpecAndroid = tabHost
.newTabSpec("Android")
.setContent(intentAndroid);
}
else if(i==1)
{
Intent intentApple = new Intent().setClass(this, Activity2.class);
TabSpec tabSpecApple = tabHost
.newTabSpec("Apple")
.setContent(intentApple);
}
else if(i==2)
{
Intent intentWindows = new Intent().setClass(this, Activity3.class);
TabSpec tabSpecWindows = tabHost
.newTabSpec("Windows")
.setContent(intentWindows);
}
else if(i==3)
{
Intent intentBerry = new Intent().setClass(this, Activity4.class);
TabSpec tabSpecBerry = tabHost
.newTabSpec("Berry")
.setContent(intentBerry);
}
}
}
Activity1.java
package com.joshclemm.android.tabs;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.TextView;
public class Activity1 extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.tab1_layout);
/* TextView tv = new TextView(this);
tv.setText("This is tab1....");
tv.setTextColor(Color.parseColor("#00B589"));*/
}
}
重複它剩下的三個佈局。
main.xml中
<TabHost
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">
<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="120dp"
android:layout_marginLeft="0dip"
android:layout_marginRight="0dip"
android:showDividers="none"
android:background="@drawable/tab_bg"
android:tabStripEnabled="false"/>
<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>
tab_item.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="@drawable/tab_bg_selector"
android:padding="10dip" android:gravity="center" android:orientation="vertical">
<ImageView
android:id="@+id/tabsImg" android:layout_width="80dp"
android:layout_height="80dp"
android:layout_gravity="center"
/>
<TextView android:id="@+id/tabsText"
android:layout_width="fill_parent"
android:layout_height="20dp"
android:textSize="15sp"
android:gravity="center|bottom"
android:textColor="@drawable/tab_text_selector" />
train_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is train tab......"/>
重複其用於其他三種佈局是這樣的...不要忘記您的所有活動添加到您的manifest.xml文件。它對我來說工作得很好。所有最好的...
相關問題
- 1. Android中的自定義選項卡
- 2. Android中的自定義選項卡
- 3. android中的自定義選項卡
- 4. 如何在自定義選項卡的每個選項卡中添加圖標?
- 5. Chrome自定義選項卡 - 意圖未被觸發(Android)
- 6. 如何在Android中創建自定義選項卡活動?
- 7. 如何在Android中自定義選項卡?
- 8. 在android中自定義選項卡指示圖像?
- 9. 如何實現自定義選項卡主機中的Android
- 10. 如何使用子視圖自定義UINavigationController自定義底部選項卡欄
- 11. 如何創建自定義選項卡在Android應用程序
- 12. 如何使用選項卡自定義設計滾動視圖?
- 13. 自定義選項卡的Android
- 14. android上的自定義選項卡
- 15. Android選項卡,如何顯示選定選項卡的圖像
- 16. 如何使用自定義中心按鈕選擇uiTabBarController中的選項卡
- 17. 在android中的自定義的操作欄選項卡
- 18. 自定義選項卡中的Java
- 19. 如何在ActionBar的導航選項卡中設置自定義視圖並使選項卡適應其高度?
- 20. 如何使用自定義視圖和選項卡創建Android ActionBar
- 21. Android應用程序中的自定義選項卡
- 22. 自定義選項卡欄與在android中的陰影背景
- 23. 使用自定義選項卡和StackNavigator?
- 24. 自定義TabWidget Android選項卡指標
- 25. Android自定義選項卡布局
- 26. 如何根據Android中的意圖更改選項卡?
- 27. 如何在我的頁面中添加自定義選項卡?
- 28. 如何在Perl的Term :: Shell中自定義選項卡完成?
- 29. 如何自定義ActionBar選項卡
- 30. 如何獲得自定義選項卡
ü應該去代替片段 –
TabActivity已經廢棄了新版本......你應該使用片段與片段活動... – Piyush