[英]TextView doesn't show inside Fragment
我有一個Fragment類,由FragmentTabHost調用,該類不顯示任何內容。
這是我的ArticulosFragment
類:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class ArticulosFragment extends Fragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.d("App", "asdasdasd");
return inflater.inflate(R.layout.fragment_articulos, container, false);
}
}
我在XML文件中有一個簡單的TextView:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Articulos"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
當我運行該應用程序時,添加的日志可以正確顯示,但文本Articulos卻沒有。
編輯:這是我的TabHost類,它顯示(或應該顯示) ArticulosFragment
類:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
tabHost.setup(this, getSupportFragmentManager(),
android.R.id.tabcontent);
inicializarTabs();
}
public void inicializarTabs() {
TabHost.TabSpec spec = tabHost.newTabSpec("Articulos");
spec.setContent(new TabContentFactory() {
@Override
public View createTabContent(String tag) {
// TODO Auto-generated method stub
return findViewById(R.id.realtabcontent);
}
});
spec.setIndicator("Articulos");
tabHost.addTab(spec, ArticulosFragment.class, null);
}
可能導致的兩件事:
嘗試從片段中刪除onCreate方法
TabContentFactory
選擇選項卡時顯示其內容。 如果需要按需創建選項卡內容,即不顯示現有視圖或開始活動,請使用此選項。
請注意這一行:
您未顯示現有視圖或未開始活動。
public abstract View createTabContent (String tag)
Callback to make the tab contents
Parameters
tag Which tab was selected.
Returns
The view to display the contents of the selected tab.
因此,因為您有一個片段,並且已經有一個視圖,所以不需要createTabContent
,有趣的部分是:
如您所見,fragmentTabHost會覆蓋TabContentFactory
。
無論如何,您的問題是:
tabHost.setup(this, getSupportFragmentManager(),android.R.id.tabcontent);
更改為:
tabHost.setup(this, getSupportFragmentManager(),R.id.realtabcontent);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.