2

シンプルな UI で 2 日間苦労しています。一番下にタブがあるタブ付きレイアウトが欲しいだけです(ActionBarSherlockその理由でタブを使用できません)。また、タブをアイコンとテキストで表現したいと考えています。ものすごく単純?Android で下のタブを使用することはお勧めできませんが、それはアプリの要件です。それについて多くを行うことはできません。

必要に応じてこの例を編集することができました。これで、下部にタブがあり、FragmentTabHost. この後、タブにアイコンを追加しようとしました。私が定期的に使用していた場合、私はTabHostできたはずです

mTabs.addTab(mTabs.newTabSpec("chapter").setIndicator("Chapter",getResources().
  getDrawable(R.drawable.chapter1)), ContentFragment.class, null);

しかし、この投稿から学びました: FragmentTabHost with drawable iconは明らかに新しいFragmentTabHost. したがって、この投稿: Icon in Tab is not shown upに従って、タブのアイコンとテキストを保持するためのカスタム ビューを実装しました。そして、それはうまくいきました。

ここでの問題 (投稿の 1 つのコメントでも尋ねられたように) は、選択がもう存在しないことです。どのタブが選択されているかわかりません。

完全なコードは次のとおりです。

MainActivity.java

public class MainActivity extends FragmentActivity {

private FragmentTabHost mTabHost;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.bottom_tabs);

    mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
    mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);

    //testing with custom view implementation to add icon
    TabSpec spec = mTabHost.newTabSpec("tab" + 1);
    View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab1_icon, null, false);
    TextView title = (TextView) tabIndicator.findViewById(R.id.title);
    title.setText("Label 1");
    ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
    Drawable myIcon = getResources().getDrawable( R.drawable.icon1 );
    icon.setImageDrawable(myIcon);
    icon.setScaleType(ImageView.ScaleType.FIT_CENTER);

    spec.setIndicator(tabIndicator);

    mTabHost.addTab(spec,
            Fragment1.class, null);

    mTabHost.addTab(mTabHost.newTabSpec("contacts")
            .setIndicator("Contacts"), Fragment2.class, null);
    mTabHost.addTab(mTabHost.newTabSpec("custom").setIndicator("Custom"),
            Fragment3.class, null);

}

}

Fragment1.java

public class Fragment1 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    View v = LayoutInflater.from(getActivity()).inflate(R.layout.tab1_view, null);
    return v;
}

bottom_tabs.xml

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

<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="@android:id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_weight="0" />
</android.support.v4.app.FragmentTabHost>

tab1_view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF0000"
android:orientation="vertical"
tools:context=".DeviceFragment" >

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Tab 1!" />

tab1_icon.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0dip"
android:layout_height="55dip"
android:layout_weight="1"
android:orientation="vertical"
android:padding="5dp"
android:weightSum="55" >

<ImageView
    android:id="@+id/icon"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="30"
    android:adjustViewBounds="false"
    android:src="@drawable/icon1" />

<TextView
    android:id="@+id/title"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="25"
    android:gravity="center_horizontal" />

4

2 に答える 2

0

使用してみてください: https://stackoverflow.com/a/23150258/2765497

11 未満の API をサポートする場合は、TabHost を FragmentTabHost に変更するだけです (サポート ライブラリまたは Sherlock から)。

于 2014-04-18T08:40:20.990 に答える