Android复合视图未显示在Fragment Tab Host上

时间:2014-06-10 02:09:35

标签: android android-tabhost

我想要一个FragmentTabHost,其ImageView中包含TextViewLinearLayoutImageViewTextView的原因,我需要更改ViewsTab Selected的选定状态。这是我的tab_view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical" >
  <ImageView
    android:id="@+id/img_tab"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_tab_alert" />
  <TextView
    android:id="@+id/txt_tab"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:singleLine="true"
    android:text="Alert" />
</LinearLayout>

我的Compound View课程为TabView

public class TabView extends FrameLayout {

public ImageView imageView;
public TextView textView;

private String textValue;
private int normalDrawableId, selectedDrawableId;

public TabView(Context context, String textValue, int normalDrawableId,
        int selectedDrawableId) {
    super(context);
    this.textValue = textValue;
    this.normalDrawableId = normalDrawableId;
    this.selectedDrawableId = selectedDrawableId;

    View view = LayoutInflater.from(context).inflate(R.layout.tab_view,
            this, false);
    textView = (TextView) view.findViewById(R.id.txt_tab);
    imageView = (ImageView) view.findViewById(R.id.img_tab);
    textView.setText(textValue);
    imageView.setImageResource(normalDrawableId);
    }
  }

Activity班,我正在做以下

public class MainActivity extends ActionBarActivity {

private FragmentTabHost fragmentTabHost;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tab_layout);
    fragmentTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
    fragmentTabHost.setup(this, getSupportFragmentManager(),
            R.id.realtabcontent);

    Bundle params = new Bundle();

    TabSpec tabSpec = fragmentTabHost.newTabSpec("Alert").setIndicator(
            createTabView(fragmentTabHost.getContext(), "Alert",
                    R.drawable.ic_tab_alert, R.drawable.ic_tab_alert_red));
    fragmentTabHost.addTab(tabSpec, TabAlertFragment.class, params);

    tabSpec = fragmentTabHost.newTabSpec("Community").setIndicator(
            createTabView(fragmentTabHost.getContext(), "Community",
                    R.drawable.ic_tab_community,
                    R.drawable.ic_tab_community_red));
    fragmentTabHost.addTab(tabSpec, TabCommunityFragment.class, params);


}

public TabView createTabView(Context context, String textValue,
        int normalDrawableId, int selectedDrawableId) {

    TabView tabView =new TabView(context,textValue,normalDrawableId,selectedDrawableId);
    return tabView;
}
}

但我无法看到任何Tabs。当我Inflate Layout来自Activity时,Tab会显示,但我无法将TabsCompound Views一起使用。我做错了什么?

1 个答案:

答案 0 :(得分:0)

好吧最后我在我的代码

中找到了罪魁祸首
  View view = LayoutInflater.from(context).inflate(R.layout.tab_view,
        this, false);

应该是

  View view = LayoutInflater.from(context).inflate(R.layout.tab_view,
        this);

通过这样做,问题得以解决