更改我的自定义操作栏标题

马赫迪·赫赖比

嘿,有人请帮我更改我的工具栏标题对齐方式,我想将其设置为中心

并在标题下画一条长色线。

这是我的 xml 布局包含在活动布局中。

我的布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 
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"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.tiger.alahedclub.activity.navigation">



<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        app:titleTextColor="@android:color/black"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/yellow"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:background="@color/yellow"
        app:tabTextColor="@android:color/black"
        app:tabSelectedTextColor="@android:color/black"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabMode="fixed"
        app:tabGravity="fill"  />

 </android.support.design.widget.AppBarLayout>
 <android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/cardview_back"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
<com.google.android.gms.ads.AdView
    android:id="@+id/adView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_alignParentBottom="true"
    ads:adSize="SMART_BANNER"
    android:layout_gravity="bottom"
    ads:adUnitId="@string/banner_ad_unit_id">

</com.google.android.gms.ads.AdView>

我的活动课:

public class navigation extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {
private TabLayout tabLayout;
private InterstitialAd mInterstitialAd;
private Button mNextLevelButton;
private TextView mTextView;
private ViewPager viewPager;
private int[] tabIcons = {
        R.drawable.ic_newsfeed,
        R.drawable.ic_livestream,
        R.drawable.ic_matches,
        R.drawable.ic_sort
};
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_navigation);


    MobileAds.initialize(getApplicationContext(), ""
    AdView mAdView = (AdView) findViewById(R.id.adView);
    AdRequest adRequest = new AdRequest.Builder().build();
    mAdView.loadAd(adRequest);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT ) != 0) {
        // Activity was brought to front and not created,
        // Thus finishing this will get us to the last viewed activity
        finish();
        return;
    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
    viewPager = (ViewPager) findViewById(R.id.viewpager);
    setupViewPager(viewPager);

    tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(viewPager);
    setupTabIcons();
}

private void setupTabIcons() {
    tabLayout.getTabAt(0).setIcon(tabIcons[0]);
    tabLayout.getTabAt(1).setIcon(tabIcons[1]);
    tabLayout.getTabAt(2).setIcon(tabIcons[2]);
 //   tabLayout.getTabAt(3).setIcon(tabIcons[3]);

}

private void setupViewPager(ViewPager viewPager) {
   ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
    adapter.addFrag(new OneFragment(),getString(R.string.newsfeed));
 //   adapter.addFrag(new TwoFragment(), getString(R.string.livestream));
    adapter.addFrag(new ThreeFragment(),getString(R.string.matches));
    adapter.addFrag(new FourFragment(),getString(R.string.schedule));
    viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();

    public ViewPagerAdapter(FragmentManager manager) {
        super(manager);
    }

    @Override
    public Fragment getItem(int position) {
        return mFragmentList.get(position);
    }

    @Override
    public int getCount() {
        return mFragmentList.size();
    }

    public void addFrag(Fragment fragment, String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentTitleList.get(position);
    }
}



@Override
public void onBackPressed() {

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {

        super.onBackPressed();
    }
}






@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    if (id == R.id.share) {

     //share();

    } else if (id == R.id.check) {
       //check();

    } else if (id == R.id.rateus) {
       // rateMe();

    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}}

在此处输入图片说明

请问有什么建议吗?

更新:

我已将此文本视图添加到工具栏标签

<TextView
 android:id="@+id/action_bar_title"                 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="@string/app_name"
            android:textColor="@android:color/black"
            android:textSize="24sp" />

但现在的问题是我得到了这个:

两个标题在同一个工具栏上被覆盖

在此处输入图片说明

更新 2:我想在应用程序标题下显示一行,如下图: 在此处输入图片说明

纳伦德拉·索拉提亚

getSupportActionbar()在下面添加一行setSupportToolbar()

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);

您只需要在工具栏下为行添加此代码,

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.v7.widget.Toolbar
       android:id="@+id/toolbar"
       app:titleTextColor="@android:color/black"
       android:layout_width="match_parent"
       android:layout_height="?attr/actionBarSize"
       android:background="@color/yellow"
       app:layout_scrollFlags="scroll|enterAlways"
       app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

   <View
       android:layout_width="match_parent"
       android:layout_height="1dp"
       android:background="@color/colorPrimary"/>

   <android.support.design.widget.TabLayout
       android:id="@+id/tabs"
       android:background="@color/yellow"
       app:tabTextColor="@android:color/black"
       app:tabSelectedTextColor="@android:color/black"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       app:tabMode="fixed"
       app:tabGravity="fill"  />

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

是否可以在操作栏上同时显示标题和自定义视图?

来自分类Dev

更改自定义操作栏标题

来自分类Dev

我可以更改自定义标题吗?

来自分类Dev

NSWindow的自定义标题栏

来自分类Dev

如何更改多个活动的自定义窗口标题栏及其图标

来自分类Dev

在自定义操作栏中显示片段标题

来自分类Dev

为什么我的自定义操作栏上的图像有多余的左填充

来自分类Dev

Sphinx:自定义侧边栏部分标题

来自分类Dev

创建自定义操作标签栏

来自分类Dev

是否可以在操作栏上同时显示标题和自定义视图?

来自分类Dev

在自定义标题栏上设置标题文本

来自分类Dev

更改自定义操作栏标题

来自分类Dev

我可以更改自定义标题吗?

来自分类Dev

为什么我的自定义样式未应用到操作栏中?

来自分类Dev

自定义图片作为标题栏中的图标

来自分类Dev

Android-自定义后,标题文本未显示在操作栏中

来自分类Dev

NSWindow标题栏自定义

来自分类Dev

QLPreviewController自定义导航栏的标题颜色吗?

来自分类Dev

如何更改多个活动的自定义窗口标题栏及其图标

来自分类Dev

Android:如何为操作栏标题提供自定义字体样式

来自分类Dev

如何自定义操作栏

来自分类Dev

如何使用自定义操作栏?

来自分类Dev

在我的自定义标题栏出现之前显示的默认标题栏

来自分类Dev

在自定义操作栏上动态更改ImageView源

来自分类Dev

我想在qt中创建一个自定义标题栏

来自分类Dev

更新自定义导航栏的标题

来自分类Dev

无法在 android 中更改自定义操作栏背景颜色

来自分类Dev

如何在 Woocommerce 我的帐户侧边栏中添加新的自定义标题?

来自分类Dev

如何使用子片段更改自定义操作栏中 textview 的文本

Related 相关文章

热门标签

归档