如何向标签添加图标

时间:2013-05-04 22:43:03

标签: java android tabs

我正在尝试向我的标签添加一个图标,但它无法正常工作,

我尝试添加

  getResources().getDrawable(R.drawable.bodyicon));

它仍然无法正常工作。当我运行应用程序时,标签栏只有文本,没有图标

我概述了我的代码所在。如果有人能弄明白为什么它不起作用会很棒。

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

public class Body extends Activity implements OnTouchListener, OnClickListener {

Button bAbs, bQuad2, bPecs;
TabHost th;
ImageView body;
final Context context = this;
StretchType pop;

// above I am defining all of the buttons and images, etc...

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(
            WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
    // here I am saying get rid of the nav bar

    setContentView(R.layout.body);
    th = (TabHost) findViewById(R.id.thbodyview);
    bAbs = (Button) findViewById(R.id.bAbss);
    bPecs = (Button) findViewById(R.id.bPecss);

    bAbs.setOnClickListener(this);
    bPecs.setOnClickListener(this);

//*********************below are my tabs*******************************************

    th.setup();
    TabSpec specs = th.newTabSpec("Front");
    specs.setContent(R.id.Front);
      specs.setIndicator("Front",getResources().getDrawable(R.drawable.bodyicon));
    th.addTab(specs);

    specs = th.newTabSpec("tag2");
    specs.setContent(R.id.Back);
    specs.setIndicator("Back");
    th.addTab(specs);

    specs = th.newTabSpec("tag3");
    specs.setContent(R.id.tab3);
    specs.setIndicator("More...");
    th.addTab(specs);

}

 //***************************Above are my tabs *******************************

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()) {
    case R.id.bAbss:
        // below I am creating the alert dialogue
        LayoutInflater li = LayoutInflater.from(context);

        View promptsView = li.inflate(R.layout.abs, null);
        // above I am setting the customview of the alert dialogue

        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                context);

        alertDialogBuilder.setView(promptsView);
        // here I am officially setting the custom layout

        // set dialog message

        alertDialogBuilder.setTitle("Stretch Type");
        // alert dialogue title

        // create alert dialog
        final AlertDialog alertDialog = alertDialogBuilder.create();
        // above is creating the alert dialogue

        final Spinner mSpinner = (Spinner) promptsView
                .findViewById(R.id.sSType);
        // above is initializing the spinner

        /*
         * dont need this button final Button mButton = (Button) promptsView
         * .findViewById(R.id.bSpinclose);
         */

        // reference UI elements from my_dialog_layout in similar fashion

        mSpinner.setOnItemSelectedListener(new OnSpinnerItemClicked());

        // show it
        alertDialog.show();
        alertDialog.setCanceledOnTouchOutside(false);
        break;

    }

}

@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
    // TODO Auto-generated method stub
    return false;
}

public class OnSpinnerItemClicked implements OnItemSelectedListener {

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int pos,
            long id) {
        switch (pos) {
        case (1):
            Intent i = new Intent(Body.this, Practice.class);
            Body.this.startActivity(i);
            break;
        }
    }

    @Override
    public void onNothingSelected(AdapterView parent) {
        // Do nothing.
    }
}

}

这是xml

<TabHost
    android:id="@+id/thbodyview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        >

           <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_alignParentLeft="true" >
            </TabWidget>

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

            <LinearLayout
                android:id="@+id/Front"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical" >

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" >

                    <LinearLayout
                        android:id="@+id/llbottomtop"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_alignParentLeft="true"
                        android:layout_centerVertical="true"
                        android:orientation="vertical" >

                        <TextView
                            android:id="@+id/tvupper"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_alignParentLeft="true"
                            android:layout_below="@+id/bAbs"
                            android:text="Upper"
                            android:textAppearance="?android:attr/textAppearanceLarge"
                            android:textColor="#FFFFFF" />

                        <View
                            android:layout_width="match_parent"
                            android:layout_height="5dp"
                            android:layout_centerVertical="true"
                            android:background="@color/orange" />

                        <TextView
                            android:id="@+id/tvlower"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_alignParentLeft="true"
                            android:layout_below="@+id/linearLayout1"
                            android:text="Lower"
                            android:textAppearance="?android:attr/textAppearanceLarge"
                            android:textColor="#FFFFFF" />
                    </LinearLayout>

                    <ImageView
                        android:id="@+id/ivBody"
                        android:layout_width="210dp"
                        android:layout_height="430dp"
                        android:layout_centerHorizontal="true"
                        android:layout_centerVertical="true"
                        android:src="@drawable/body_front" />

                    <Button
                        android:id="@+id/bPecss"
                        android:layout_width="83dp"
                        android:layout_height="50dp"
                        android:layout_above="@+id/bAbss"
                        android:layout_alignRight="@+id/bAbss"
                        android:text="Pecs" />

                    <Button
                        android:id="@+id/bAbss"
                        android:layout_width="80dp"
                        android:layout_height="77dp"
                        android:layout_alignBottom="@+id/llbottomtop"
                        android:layout_centerHorizontal="true"
                        android:layout_marginBottom="42dp"
                        android:text="Abs" />
                </RelativeLayout>
            </LinearLayout>

            <LinearLayout
                android:id="@+id/Back"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >


                   <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" >

                    <LinearLayout
                        android:id="@+id/llbottomtop"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_alignParentLeft="true"
                        android:layout_centerVertical="true"
                        android:orientation="vertical" >

                        <TextView
                            android:id="@+id/tvupper"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_alignParentLeft="true"
                            android:layout_below="@+id/bAbs"
                            android:text="Upper"
                            android:textAppearance="?android:attr/textAppearanceLarge"
                            android:textColor="#FFFFFF" />

                        <View
                            android:layout_width="match_parent"
                            android:layout_height="5dp"
                            android:layout_centerVertical="true"
                            android:background="@color/orange" />

                        <TextView
                            android:id="@+id/tvlower"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_alignParentLeft="true"
                            android:layout_below="@+id/linearLayout1"
                            android:text="Lower"
                            android:textAppearance="?android:attr/textAppearanceLarge"
                            android:textColor="#FFFFFF" />
                    </LinearLayout>

                    <ImageView
                        android:id="@+id/ivBody"
                        android:layout_width="210dp"
                        android:layout_height="430dp"
                        android:layout_centerHorizontal="true"
                        android:layout_centerVertical="true"
                        android:src="@drawable/body_back" />


                </RelativeLayout>

            </LinearLayout>

            <LinearLayout
                android:id="@+id/tab3"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >
            </LinearLayout>



        </FrameLayout>
    </RelativeLayout>
</TabHost>

2 个答案:

答案 0 :(得分:1)

我的建议是访问标签标题textview并设置left | top | right | bottom drawable

示例:

//to get first tab title as textView from tabHost
TextView tabTitle = (TextView) tabHost.getTabWidget().getChildAt(0)
                        .findViewById(android.R.id.title);
//to set the icon to left of the tab title
tabTitle.setCompoundDrawablesWithIntrinsicBounds(
                    getActivity().getResources().getDrawable(
                            R.drawable.ic_tab_icon), null, null, null);

答案 1 :(得分:0)

我有两个物理设备一个2.2和另一个4.2.2。在2.2上没有问题,图标出现。在4.2.2上没有图标。有些标签在Android中的标签布局方面发生了变化。如果你真的需要使用TabHost我看到你可以传递一个视图作为tabindicator。这段代码有点像伪代码,因为在布局方面它仍然在UI中进行了一些调整,但试试这个:

定义名为tabindicator_layout.xml的布局并将其放在res / layout中。其内容如下:

<?xml version="1.0" encoding="utf-8"?>

<ImageView
    android:id="@+id/image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal|top"
    android:scaleType="center" >
</ImageView>

<TextView
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal" />

然后在你的类代码中运行时膨胀这个视图并用你想要的东西填充图像和textview。为每个标签执行此操作:

//*********************below are my tabs*******************************************

    th.setup();
    TabSpec specs = th.newTabSpec("Front");
    specs.setContent(R.id.Front);

    View tabIndicator = LayoutInflater.from(this).inflate(
            R.layout.tabindicator_layout, th.getTabWidget(), false);
    ((TextView) tabIndicator.findViewById(R.id.title)).setText("Front");
    ((ImageView) tabIndicator.findViewById(R.id.image))
            .setImageResource(R.drawable.ic_launcher);

    specs.setIndicator(tabIndicator);

    TabSpec specs2 = th.newTabSpec("tag2");
    specs2.setContent(R.id.Back);

    View tabIndicator2 = LayoutInflater.from(this).inflate(
            R.layout.tabindicator_layout, th.getTabWidget(), false);
    ((TextView) tabIndicator2.findViewById(R.id.title)).setText("Back");
    ((ImageView) tabIndicator2.findViewById(R.id.image))
            .setImageResource(R.drawable.ic_launcher);

    specs2.setIndicator(tabIndicator2);

    TabSpec specs3 = th.newTabSpec("tag3");
    specs3.setContent(R.id.tab3);

    View tabIndicator3 = LayoutInflater.from(this).inflate(
            R.layout.tabindicator_layout, th.getTabWidget(), false);

    ((TextView) tabIndicator3.findViewById(R.id.title)).setText("...More");
    ((ImageView) tabIndicator3.findViewById(R.id.image))
            .setImageResource(R.drawable.ic_launcher);

    specs3.setIndicator(tabIndicator3);

    th.addTab(specs);
    th.addTab(specs2);
    th.addTab(specs3);

当然用你要显示的任何图标替换R.drawable.ic_launcher

另一个有趣的事情可能有所帮助..我注意到,如果我传入一个空白的标题,原来的方式有效,但没有显示标题。所以你可以进入photoshop,只需将标题添加到图像中,使其更好看。试试这个:

TabSpec specs = th.newTabSpec("Front");
    specs.setContent(R.id.Front);
      specs.setIndicator("",getResources().getDrawable(R.drawable.ic_launcher));
    th.addTab(specs);