我試圖創建一個底部tabhost +片段。當我點擊tabhost時,標籤內容不會改變
的問題是,
1)當我按下標籤按鈕,它不會切換到另一個片段,雖然我的日誌顯示,該公司已經在創建視圖輸入。我厭倦了放在每個片段上的文本視圖,但沒有顯示,所以我懷疑片段沒有添加framelayout id:realtabcontent?
2)底部的標籤似乎主機「太底」,使得一些標籤的底部 的是在屏幕
的如何解決這些問題?感謝您的幫助
主要活動
public class MainActivity extends FragmentActivity {
public FragmentTabHost tabHost;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabHost = (FragmentTabHost) findViewById(R.id.tabhost);
tabHost.setup(this, getSupportFragmentManager(), R.id.tabcontent);
tabHost.addTab(
setIndicator(this, tabHost.newTabSpec("About"),
R.drawable.ic_launcher), About.class, null);
tabHost.addTab(
setIndicator(this, tabHost.newTabSpec("Camera"),
R.drawable.ic_launcher), CameraHandle.class, null);
tabHost.addTab(
setIndicator(this, tabHost.newTabSpec("Gallery"),
R.drawable.ic_launcher), PhotoGallery.class, null);
tabHost.addTab(
setIndicator(this, tabHost.newTabSpec("LeaderBoard"),
R.drawable.ic_launcher), LeaderBoard.class, null);
}
public TabSpec setIndicator(Context ctx, TabSpec spec, int resId) {
// TODO Auto-generated method stub
View v = LayoutInflater.from(ctx).inflate(R.layout.custom_tab, null);
ImageView logo = (ImageView) v.findViewById(R.id.tab_logo);
logo.setImageResource(resId);
TextView text = (TextView) v.findViewById(R.id.tab_title);
text.setText(spec.getTag());
return spec.setIndicator(v);
}
}
XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@+id/realtabcontent"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1" />
<android.support.v4.app.FragmentTabHost
android:id="@+id/tabhost"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<FrameLayout
android:id="@+id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0" />
</android.support.v4.app.FragmentTabHost>
</LinearLayout>
片段(對於每個選項卡一個片段)
public class About extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.about, container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
}
XML:
<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="TextVsssiew" />
</LinearLayout>
但該選項卡是在佈局的頂部,但沒有佈局的底部? – user782104