定制的Android TabWidget指示标签 [英] Custom TabWidget Android Tab Indicator
问题描述
好了,所以我让有标签,现在我的问题是,标签控件是不能跨越有多种不同版本的Android或设备的统一Android应用程序。
我想使之成为在任何Android一样,这是我的标签活动
Okay, so i am making an android app that has tabs, now my problem is that the tab widget isn't uniform across the diffrent android versions or devices. I want to make it to be the same on any android this is my tab activity
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
public class Cook extends TabActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cook_layout);
TabHost tabHost = getTabHost();
// Tab for Snacks
TabSpec snackspec = tabHost.newTabSpec("Snacks");
// setting Title and Icon for the Tab
snackspec.setIndicator("Snacks", getResources().getDrawable(R.drawable.cook_icon_tab_snacks));
Intent snacksIntent = new Intent(this, Cook_tab_snacks.class);
snackspec.setContent(snacksIntent);
// Tab for Mains
TabSpec mainspec = tabHost.newTabSpec("Mains");
mainspec.setIndicator("Mains", getResources().getDrawable(R.drawable.cook_icon_tab_snacks));
Intent mainsIntent = new Intent(this, Cook_tab_mains.class);
mainspec.setContent(mainsIntent);
// Tab for Desserts
TabSpec dessertspec = tabHost.newTabSpec("Desserts");
dessertspec.setIndicator("Desserts", getResources().getDrawable(R.drawable.cook_icon_tab_snacks));
Intent dessertsIntent = new Intent(this, Cook_tab_desserts.class);
dessertspec.setContent(dessertsIntent);
// Adding all TabSpec to TabHost
tabHost.addTab(snackspec); // Adding snacks tab
tabHost.addTab(mainspec); // Adding mains tab
tabHost.addTab(dessertspec); // Adding desserts tab
}
}
我也有我的XML布局:
I also have my XML layout :
<?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"
>
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<FrameLayout
android:id="@android:id/tabcontent"
android:background="@drawable/gradient_bg"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
我做了一个新的指标XML这就好比是主要的Android选项卡指示器V4
我跟着搜查博客很多,我无法找到我的答案...
我真想让android的选项卡统一在所有的Android版本,并使颜色好看,因为橙色和黄色真的不与我的应用程序的颜色主题契合
请帮助!!!!
我似乎无法找到一个方法来解决它?
干杯
I made a new indicator xml which is like the main android tab indicator v4 I followed and searched alot of blogs , i couldn't find my answer ... I really want to make the android tabs uniform across all android versions and to make the colors nice , since orange and yellow dont really fit with the color theme in my app Help please!!!! I cant seem to find a way to fix it... Cheers
推荐答案
好吧,我找到了解决办法。
这里是code:
okay i found a solution. here is the code:
import android.app.TabActivity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
public class Cook extends TabActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cook_layout);
TabHost tabHost = getTabHost();
// Tab for Snacks
TabSpec snackspec = tabHost.newTabSpec("Snacks");
// setting Title and Icon for the Tab
snackspec.setIndicator(makeTabIndicator(getResources().getDrawable(R.drawable.cook_icon_tab_snacks)));
Intent snacksIntent = new Intent(this, Cook_tab_snacks.class);
snackspec.setContent(snacksIntent);
// Tab for Mains
TabSpec mainspec = tabHost.newTabSpec("Mains");
mainspec.setIndicator(makeTabIndicator( getResources().getDrawable(R.drawable.cook_icon_tab_snacks)));
Intent mainsIntent = new Intent(this, Cook_tab_mains.class);
mainspec.setContent(mainsIntent);
// Tab for Desserts
TabSpec dessertspec = tabHost.newTabSpec("Desserts");
dessertspec.setIndicator(makeTabIndicator( getResources().getDrawable(R.drawable.cook_icon_tab_snacks)));
Intent dessertsIntent = new Intent(this, Cook_tab_desserts.class);
dessertspec.setContent(dessertsIntent);
// Adding all TabSpec to TabHost
tabHost.addTab(snackspec); // Adding snacks tab
tabHost.addTab(mainspec); // Adding mains tab
tabHost.addTab(dessertspec); // Adding desserts tab
}
//making the tab view:
private View makeTabIndicator(Drawable drawable){
ImageView Tabimage = new ImageView(this);
LayoutParams LP = new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT,1);
LP.setMargins(1, 0, 1, 0);
Tabimage.setLayoutParams(LP);
Tabimage.setImageDrawable(drawable);
Tabimage.setBackgroundResource(R.drawable.tabview);
return Tabimage;
}}
我不知道天气或没有我需要的cook_layout了我去看看,如果我可以将其删除或更新版本离开它...现在我只想把一切工作,我稍后会为前来一轮干净,共度了
希望就这个问题,可以帮助你们在那里,失足!欢呼声
I dont know weather or not i need the cook_layout anymore i'll see if i can remove it or leave it later... right now i just want to get it all working and later i'll come round by for a clean and tiding up hope that helps you guys out there that stumble upon this question! cheers
这篇关于定制的Android TabWidget指示标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!