2013-07-03 71 views
1

我正在研究android应用程序有tabs.I已完成实施,但我面临一个问题。图像不适合选项卡。Android选项卡图像不适合选项卡

这里是我的Java代码:

import android.app.TabActivity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.widget.ImageView; 
import android.widget.TabHost; 
import android.widget.TabHost.TabSpec; 
import android.widget.TextView; 

import com.tv.allies.FriendsScreen; 
import com.tv.servercommunication.IServerResponse; 
import com.tv.setting.SettingActivity; 
import com.tv.socialgoal.R; 
import com.tv.socialgoal.profile.ProfileScreen; 
import com.tv.task.TaskListActivity; 

@SuppressWarnings("deprecation") 
public class TabViewActivity extends TabActivity implements IServerResponse{ 
    private static final String INBOX_SPEC = "Inbox"; 

    private static final String OUTBOX_SPEC = "Outbox"; 
    private static final String PROFILE_SPEC = "Profile"; 
    private static final String SETTING_SPEC = "Setting"; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     TabHost tabHost = getTabHost(); 
     tabHost.addTab(tabHost 
       .newTabSpec("one") 
       .setIndicator("", 
         getResources().getDrawable(R.drawable.missions_btn_up)) 
         .setContent(
           new Intent(this, TaskListActivity.class) 
           .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))); 
     TabSpec outboxSpec = tabHost.newTabSpec(OUTBOX_SPEC); 

     tabHost.addTab(tabHost 
       .newTabSpec("two") 
       .setIndicator("", 
         getResources().getDrawable(R.drawable.allies_btn_up)) 
         .setContent(new Intent(this, FriendsScreen.class))); 

     TabSpec setting = tabHost.newTabSpec(OUTBOX_SPEC); 
     outboxSpec.setIndicator("",getResources().getDrawable(R.drawable.profile_btn_up)); 
     Intent outboxIntent = new Intent(this, ProfileScreen.class); 
     outboxSpec.setContent(outboxIntent); 

     // Profile Tab 
     TabSpec profileSpec = tabHost.newTabSpec(PROFILE_SPEC); 
     profileSpec.setIndicator("",getResources().getDrawable(R.drawable.settings_btn_up)); 
     Intent profileIntent = new Intent(this, SettingActivity.class); 
     profileSpec.setContent(profileIntent); 

     tabHost.addTab(outboxSpec); // Adding Outbox tab 
     tabHost.addTab(profileSpec); 
    } 

    @Override 
    public void serverResponse(String response, int processid) { 
     // TODO Auto-generated method stub 

    } 

} 

这里是标签的xml:

<?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:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:orientation="vertical" > 

     <TabWidget 
      android:id="@android:id/tabs" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:background="#3399FF"/> 

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

</TabHost> 

这里是屏幕短:

enter image description here

所有标签图像如何适合每个标签?

回答

0

根据分辨率在标签中使用正确的图像

使用以下链接生成标签图像。 [check this]

+0

嗨我正在使用正确的图像标签。 –

+0

@ricintech检查此http://www.androidhive.info/2011/08/android-tab-layout-tutorial/ – sachin10

2

回答你的问题是一件棘手的事情。对于设置标签的背景和图像,根据您的要求,你必须使用你必须对你的标签

1)对于标签的背景设置图像的自定义XML: -

一)创建一个XML您可绘制文件夹,复制粘贴以下

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android">  
    <item android:state_selected="true" android:drawable="@drawable/tab_bg_green" /> 
    <item android:drawable="@drawable/tab_bg_blue" /> 
</selector> 

这个XML将工作一样,我们使用的按钮XML。在按下状态和正常状态下。 名称这个XML作为tab_bg.xml

b)中在Java文件使用在代码下面的方法和其次通过您tabhost例如在该方法中

public void setTabColor(TabHost tabhost) { 
     for(int i=0;i<tabhost.getTabWidget().getChildCount();i++) 
     { 
      tabhost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.tab_bg); //unselected 
     } 
    } 

2)的标签图标以下为tabhost使用: - 使用xml像

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <!-- When selected, use grey --> 
    <item android:drawable="@drawable/settings_btn_down" 
      android:state_selected="true" /> 
    <!-- When not selected, use white--> 
    <item android:drawable="@drawable/settings_btn_up" /> 
</selector> 

命名此XML作为settings_tab_xml.xml。并在您的代码

tabHost.addTab(tabHost 
       .newTabSpec("two") 
       .setIndicator("", 
         getResources().getDrawable(R.xml.settings_tab_xml)) 
         .setContent(new Intent(this, FriendsScreen.class))); 

让我知道,如果这可以帮助你。

相关问题