0

我正在尝试在我的标签中添加一个图标,但它不起作用,

我尝试添加

  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>
4

2 回答 2

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);
于 2014-04-19T12:37:47.470 回答
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" />

然后在运行时在您的类代码中填充此视图并使用您想要的填充图像和文本视图。为每个选项卡执行此操作:

//*********************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);
于 2013-05-04T23:50:27.423 回答