1

我的项目中有一个寻呼机标签条,当在模拟器或设备上运行时,它会像这样开始居中

在此处输入图像描述

我想要实现的是让标签开始像这样

在此处输入图像描述

我已经尝试在我的 xml 中留下布局重力,但这对我不起作用,我无法解决这个问题,这是我的带有寻呼机标签条的 xml。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">


    <android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/pager"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
        tools:context=".MainActivity" >

        <android.support.v4.view.PagerTabStrip
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:background="@drawable/gradient_bg"
            android:textColor="#fff"
            android:textSize="20sp"
            android:id="@+id/tab_strip"
            android:focusableInTouchMode="false">



        </android.support.v4.view.PagerTabStrip>


</android.support.v4.view.ViewPager>




</LinearLayout>

爪哇代码

import android.graphics.Color;
import android.graphics.drawable.AnimationDrawable;
import android.media.MediaPlayer;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.PagerTabStrip;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;


public class MainActivity extends FragmentActivity {

    //adview


    ViewPager pager;
PagerTabStrip tab_strp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_tab);
        ma_pager_adapter mapager=new ma_pager_adapter(getSupportFragmentManager());
        pager=(ViewPager)findViewById(R.id.pager);
        pager.setAdapter(mapager);
tab_strp=(PagerTabStrip)findViewById(R.id.tab_strip);
tab_strp.setTextColor(Color.WHITE);

     //   tab_strp.setTextSize(14,14);
       // tab_strp.setTabIndicatorColor(Color.WHITE);

        final MediaPlayer mp = MediaPlayer.create(this, R.raw.firsteps);
        mp.start();

    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        if(hasFocus){
            ImageView welcomeText = (ImageView)findViewById(R.id.btsMainTextImage);
            //welcomeText.animate().rotation(1440f).setDuration(2000);


        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.share) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }




}

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;


import java.util.Locale;


//final MediaPlayer mp = MediaPlayer.create(this, R.raw.short_whoosh2);

    public ma_pager_adapter(FragmentManager fm) {
        super(fm);


    }
   //final MediaPlayer mp = MediaPlayer.create(this, R.raw.short_whoosh2);



    @Override
    public Fragment getItem(int i) {
        
        switch (i) {
            case 0:
                tab1 t1 = new tab1();
                return t1;
            case 1:
                tab2 t2 = new tab2();
                return t2;
            case 2:
                tab4 t4 = new tab4();
                return t4;
            case 3:
                tab3 t3 = new tab3();
                return t3;
            case 4:
                tab5 t5 = new tab5();
                return t5;

        }
        return null;
    }



    @Override
    public int getCount() {
        return 5;
    }//set the number of tabs





    @Override
    public CharSequence getPageTitle(int position) {
       Locale l = Locale.getDefault();
        switch (position) {
            case 0:

                return "Tab1";
            case 1:

                return "Tab2";
            case 2:

               return "Tab3";

            case 3:

                return "Tab4";

            case 4:

                return "Tab5";

        }
        return null;
    }



}

4

2 回答 2

2

这对于 PagerTabStrip 和 ToolBar 是不可能的。由于 ActionBarTabs 已被弃用,请使用 TabLayout。

于 2016-07-01T08:19:02.540 回答
0

这是如何轻松使用 TabLayout 的代码。

这是您将获得的输出。 在此处输入图像描述

main_activty.xml或您想要使用这些选项卡和 Viewpager 的任何 XML 文件

<android.support.design.widget.AppBarLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" android:id="@+id/appBarLayout"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent">

    <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabTextColor="#f9f8f8"
            app:tabSelectedTextColor="#fff"
            app:tabGravity="fill"
            app:tabMode="fixed"/>

</android.support.design.widget.AppBarLayout>

<android.support.v4.view.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_gravity="left"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        app:layout_constraintTop_toBottomOf="@+id/appBarLayout" app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toBottomOf="parent">


</android.support.v4.view.ViewPager>

该 XML 代码应该适合您。有关完整代码,您可以访问此处 - https://tutorialvilla.com/how/how-to-use-tablayout-and-viewpager-in-android-kotlin

于 2019-07-11T04:38:34.483 回答