Android:在底部显示标签

时间:2015-10-06 13:43:30

标签: android tabs android-tabhost

我正在尝试显示屏幕底部的标签。我有以下代码,它在设计预览和模拟器的底部显示选项卡,但是当我在真实设备上测试代码时,选项卡显示在顶部。无法理解原因。我已经尝试过stackoverflow和其他网站上的答案,但是没有用

Android: Tabs at the BOTTOM

How to show tabs At Bottom rather then at top?

Android having tabs at the bottom of screen?

这是代码:

<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:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="5dp">

    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:layout_weight="1"/>

    <TabWidget
        android:id="@android:id/tabs"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0"
        android:layout_marginBottom="-4dp"
        android:gravity="center_vertical|center_horizontal|bottom" />

</LinearLayout>

这是我的活动代码:

public class AndroidTabLayoutActivity extends TabActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        TabHost tabHost = getTabHost();

        // Tab for Photos
        TabHost.TabSpec photospec = tabHost.newTabSpec("");
        // setting Title and Icon for the Tab
        photospec.setIndicator("", getResources().getDrawable(R.drawable.icon_photos_tab));
        Intent photosIntent = new Intent(this, PhotosActivity.class);
        photospec.setContent(photosIntent);

        // Tab for Songs
        TabHost.TabSpec songspec = tabHost.newTabSpec("");
        songspec.setIndicator("", getResources().getDrawable(R.drawable.icon_songs_tab));
        Intent songsIntent = new Intent(this, SongsActivity.class);
        songspec.setContent(songsIntent);

        //Tab for Lists
        TabHost.TabSpec listspec = tabHost.newTabSpec("");
        listspec.setIndicator("", getResources().getDrawable(R.drawable.icon_lists_tab));
        Intent listIntent = new Intent(this, ListsActivity.class);
        listspec.setContent(listIntent);

        // Tab for Videos
        TabHost.TabSpec videospec = tabHost.newTabSpec("");
        videospec.setIndicator("", getResources().getDrawable(R.drawable.icon_videos_tab));
        Intent videosIntent = new Intent(this, VideosActivity.class);
        videospec.setContent(videosIntent);

        // Adding all TabSpec to TabHost
        tabHost.addTab(photospec); // Adding photos tab
        tabHost.addTab(songspec); // Adding songs tab
        tabHost.addTab(listspec); //Adding lists tab
        tabHost.addTab(videospec); // Adding videos tab

        // Set drawable images to tab
        tabHost.getTabWidget().getChildAt(1).setBackgroundColor(Color.parseColor("#1D2023"));
        tabHost.getTabWidget().getChildAt(2).setBackgroundColor(Color.parseColor("#1D2023"));
        tabHost.getTabWidget().getChildAt(3).setBackgroundColor(Color.parseColor("#1D2023"));

        // Set Tab1 as Default tab and change image
        tabHost.getTabWidget().setCurrentTab(0);
        tabHost.getTabWidget().getChildAt(0).setBackgroundColor(Color.parseColor("#1D2023"));
    }
     }

任何帮助将不胜感激......

0 个答案:

没有答案