2013-05-04 81 views
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> 
+0

你必须使用TabHost,它是唯一的Android组件我讨厌现在存在有动作条标签... – 2013-05-04 22:46:50

+0

您可以张贴在tabhost定义的XML? – j2emanue 2013-05-04 22:48:45

+0

@ j2emanue我加入了XML – user2109242 2013-05-04 22:55:47

回答

0

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

定义布局称为tabindicator_layout.xml并把它在res /布局。其内容是这样的:

<?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); 

当然你要显示

,如果我在一个空白的传递可能help..i通知另一个有趣的事情任何图标替换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); 
+0

@ j23manue是第二种方式(离开标题空白)的作品感谢 – user2109242 2013-05-05 03:26:08

+0

如果答案是你可以接受它作为一个答案可以接受的。 – j2emanue 2013-05-05 17:47:12

0

我的技巧是可以访问的分页标题的TextView和设置左|顶|右|底部绘制

例子:

//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);