简体   繁体   中英

Android Right Align TabWidget

I want use tabs in my own android app but there is a problem for me. I try to do TabWidget alignment at right side so as first tab header placed at right side. I find some solution in web like setting TabWidget gravity to right , nested TabWidget in LinearLayout and setting it gravity to right, but them did't work.

This is my code :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="ir.andrun.tabbased.Main"
    tools:showIn="@layout/app_bar_main">

    <TabHost
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/tabHost">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <HorizontalScrollView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:scrollbars="none">
                <TabWidget
                    android:id="@android:id/tabs"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                        <TextView
                            android:tag="tab0"
                            android:text="Tab 1"
                            android:layout_width="wrap_content"
                            android:layout_height="fill_parent" />
                        <TextView
                            android:tag="tab1"
                            android:text="Tab 2"
                            android:layout_width="wrap_content"
                            android:layout_height="fill_parent" />
                        <TextView
                            android:tag="tab2"
                            android:text="Tab 3"
                            android:layout_width="wrap_content"
                            android:layout_height="fill_parent" />

                </TabWidget>
            </HorizontalScrollView>
            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <LinearLayout
                    android:id="@+id/linearLayout"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical">

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

                </LinearLayout>

                <LinearLayout
                    android:id="@+id/linearLayout2"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical">

                    <TextView
                        android:text="Tab 2"
                        android:layout_width="wrap_content"
                        android:layout_height="fill_parent" />

                </LinearLayout>

                <LinearLayout
                    android:id="@+id/linearLayout3"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical">

                    <TextView
                        android:text="Tab 3"
                        android:layout_width="wrap_content"
                        android:layout_height="fill_parent" />

                </LinearLayout>
            </FrameLayout>
        </LinearLayout>
    </TabHost>
</RelativeLayout>

This is result :

在此处输入图片说明

Pleas somebody help me.

Tanks.

Why use HorizontalScrollView ?

Use Android Material Design working with Tabs

Option 0: Try setting the tabhost to android:gravity="end"

Option 1: Try setting the tabhost to android:layout_alignRight="true" or android:layoutParentEnd="true"

Option 2: Add a space taker-upper before tabhost

<!-- Take up space -->
   <LinearLayout
       android:orientation="horizontal"
       android:layout_width="0dp"
       android:layout_height="match_parent"
       android:layout_weight="0.1"/>

Hope one of those is what you are looking for.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM