本文介绍了如何使图标位于“选项卡布局"中“文本"的左侧?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚如何让图标不在文本的顶部,而是在文本的侧面,但是我不确定如何使用我的代码来实现.我是Android Studio的新手,所以我将不胜感激.

I am trying to figure out how to have the icons not be on top of the text but rather to the side of it, but I am not sure how to do it with my code specifically. I am quite new to Android Studio so I would be grateful for any help.

XML代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".MyCabinet">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbarMyCabinet"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:background="@color/colorPrimary">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="@string/teacher_files_title"
            android:gravity = "center"
            android:id="@+id/toolbar_title"
            app:fontFamily="@font/capriola"
            android:textSize="20sp"
            />

    </android.support.v7.widget.Toolbar>

    <android.support.design.widget.TabLayout
        android:id="@+id/tabLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabMode="fixed"
        app:tabGravity="fill"
        app:tabIndicatorColor="@color/colorAccent"
        app:elevation="4dp">
    </android.support.design.widget.TabLayout>

    <android.support.v4.view.ViewPager
        android:layout_width="match_parent"
        android:id="@+id/viewPagerMyCabinet"
        android:layout_height="match_parent">

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

</LinearLayout>

活动代码

private TabLayout tabLayout;
private ViewPager viewPager;
private ViewPagerAdapter adapter;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my_cabinet);

    tabLayout = (TabLayout) findViewById(R.id.tabLayout);
    viewPager = (ViewPager) findViewById(R.id.viewPagerMyCabinet);
    adapter = new ViewPagerAdapter(getSupportFragmentManager());

    //add fragment here

    adapter.AddFragment(new FragmentMyCabinet(), "My Cabinet");
    adapter.AddFragment(new FragmentUpload(), "Upload");
    adapter.AddFragment(new FragmentRecent(), "Recent");
    viewPager.setAdapter(adapter);
    tabLayout.setupWithViewPager(viewPager);
    tabLayout.getTabAt(0).setIcon(R.drawable.ic_folder);
    tabLayout.getTabAt(1).setIcon(R.drawable.ic_camera);
    tabLayout.getTabAt(2).setIcon(R.drawable.ic_clock);

    android.support.v7.widget.Toolbar toolbar = findViewById(R.id.toolbarMyCabinet);
    setSupportActionBar(toolbar);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu, menu);
    return true;
}

ViewPagerAdapter

import android.support.annotation.Nullable;
import android.support.v4.app.FragmentPagerAdapter;

import java.util.ArrayList;
import java.util.List;

public class ViewPagerAdapter extends FragmentPagerAdapter {

    private final List<android.support.v4.app.Fragment> lstFragment = new ArrayList<>();

    private final List<String> lstTitles = new ArrayList<>();

    public ViewPagerAdapter(android.support.v4.app.FragmentManager fm) {
        super(fm);
    }

    @Override
    public android.support.v4.app.Fragment getItem(int position) {
        return lstFragment.get(position);
    }

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

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


    public void AddFragment(android.support.v4.app.Fragment fragment, String title) {

        lstFragment.add(fragment);
        lstTitles.add(title);

    }
}

用户界面如下所示:应用的用户界面

我不确定是否必须创建自定义xml,如果不确定,我不太确定如何将其顺利实现到现在的代码中.

I am not sure if I would have to create a custom xml or not, and if I did I am not too sure how to implement it smoothly into the code I have now.

推荐答案

添加 app:tabInlineLabel ="true" 确实对我有用.

<com.google.android.material.tabs.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:tabMode="fixed"
            app:tabGravity="fill"
            app:tabInlineLabel="true"
            app:tabIndicatorHeight="0dp"/>

这篇关于如何使图标位于“选项卡布局"中“文本"的左侧?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 01:48