2014-09-23 40 views
0

我将包含ImageViews的自定义视图添加到标签。向标签添加图标后性能下降

在这里,我得到的布局:

 LinearLayout view = (LinearLayout) getLayoutInflater().inflate(
       R.layout.custom_actionbar_tab_1, null); 
     ImageView image1 = (ImageView) view.findViewById(R.id.image1); 
     image1.setImageResource(R.drawable.icon_28799); 

     LinearLayout view2 = (LinearLayout) getLayoutInflater().inflate(
       R.layout.custom_actionbar_tab_2, null); 
     ImageView image2 = (ImageView) view2.findViewById(R.id.image2); 
     image2.setImageResource(R.drawable.icon_20321); 

     LinearLayout view3 = (LinearLayout) getLayoutInflater().inflate(
       R.layout.custom_actionbar_tab_3, null); 
     ImageView image3 = (ImageView) view3.findViewById(R.id.image3); 
     image3.setImageResource(R.drawable.icon_39547); 

在这里,我设置自定义视图选项卡:

 actionBar.addTab(actionBar.newTab().setCustomView(view) 
       .setTabListener(this)); 

     actionBar.addTab(actionBar.newTab().setCustomView(view2) 
       .setTabListener(this)); 

     actionBar.addTab(actionBar.newTab().setCustomView(view3) 
       .setTabListener(this)); 

但是当我测试它在模拟器应用迅速放缓,它永远在标签之间滑动。图标位于一个可绘制的文件夹中。有人有类似的问题?

+0

当测试性能时,我不确定我会信任模拟器。您是否在实际设备上进行了测试? – Knossos 2014-09-23 15:26:05

+0

我使用genymotion,这对于模拟器来说非常快速...因为我将图标添加到了应用程序的标签缓慢。当我删除图标时,速度很快。 – Sini 2014-09-23 15:47:39

+0

好吧我找到了。这是图像尺寸减慢了性能。我调整了每个denisty的大小 – Sini 2014-09-23 15:51:07

回答

0

问题是由图像大小造成的。我调整了它的每一个denisty并解决了这个问题。

0

尝试下面我的代码与图标/文本标签,确保您在TabActivity

延长
private void setTabs() 
{ 
    addTab("Tab1", R.drawable.icon1, Tab1.class); 
    addTab("Tab2", R.drawable.icon2, Tab2.class); 
    addTab("Tab3", R.drawable.icon3, Tab3.class); 
    addTab("Tab4", R.drawable.icon4, Tab4.class); 
    addTab("Tab5", R.drawable.icon5, Tab5.class); 
} 

private void addTab(String labelId, int drawableId, Class<?> c) 
{ 
    TabHost tabHost = getTabHost(); 
    Intent intent = new Intent(this, c); 
    TabHost.TabSpec spec = tabHost.newTabSpec("tab" + labelId); 

    View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator, getTabWidget(), false); 
    TextView title = (TextView) tabIndicator.findViewById(R.id.title); 
    title.setText(labelId); 
    ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon); 
    icon.setImageResource(drawableId); 

    spec.setIndicator(tabIndicator); 
    spec.setContent(intent); 
    tabHost.addTab(spec); 
} 

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

    <FrameLayout 
     android:id="@android:id/tabcontent" 
     android:layout_width="fill_parent" 
     android:layout_height="0dip" 
     android:layout_weight="1" /> 

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

</TabHost> 

然后应用自己的风格标签。这基本上就是这样。如果你仍然在模拟器上遇到崩溃,我建议你尝试genymotion。

+0

我已经在使用genymotion :-) – Sini 2014-09-23 15:54:36