我的项目中有一个寻呼机标签条,当在模拟器或设备上运行时,它会像这样启动中心
我想要实现的是让标签像这样开始
我已尝试在我的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>
java代码
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;
}
}
答案 0 :(得分: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
答案 1 :(得分:-1)
使用PagerTabStrip和ToolBar无法做到这一点。由于不推荐使用ActionBarTabs,请使用TabLayout。