我没有获得自定义标签的图标

时间:2011-10-04 11:14:10

标签: java android android-layout

在我的应用中,我正在使用自定义标签。在这里,我将以图形方式将图标添加到图像视图中。除第一个选项卡外,图像正确添加。请任何人帮助我。 以下是我的代码。

<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="2dip" 
    android:gravity="center" 
    android:orientation="vertical">

    <ImageView android:id="@+id/tabicon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"        
    />
    <TextView android:id="@+id/tabsText" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:text="Title"
        android:textSize="12dip" 
        android:textColor="@drawable/tab_text_selector"
    />

</LinearLayout>

Java代码

public class CustomTabActivity extends Activity {

    private TabHost mTabHost;   
    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);
        // construct the tabhost
        setContentView(R.layout.main);

        setupTabHost();
        mTabHost.getTabWidget().setDividerDrawable(R.drawable.tab_divider);

        setupTab(new TextView(this), "Find Gifts",R.drawable.findgifts);
        setupTab(new TextView(this), "Tracked Items",R.drawable.items);
        setupTab(new TextView(this), "Preferences",R.drawable.preferences);
    }

    private void setupTab(final View view, final String tag,int drawable) {
        View tabview = createTabView(mTabHost.getContext(), tag, drawable);

        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,int drawable) {
        View view = LayoutInflater.from(context).inflate(R.layout.tabs_bg, null);
        ImageView tabicon = (ImageView) view.findViewById(R.id.tabicon);    
        tabicon.setBackgroundResource(drawable);
        TextView tv = (TextView) view.findViewById(R.id.tabsText);
        tv.setText(text);       
        return view;
    }
}

1 个答案:

答案 0 :(得分:1)

尝试使用

形式

为make选项卡提供图像

http://www.codeproject.com/KB/android/AndroidTabs.aspx