如何设置选项卡中的图像/图标而不是文本? [英] How to set Image/Icon in Tabs instead of Text?
本文介绍了如何设置选项卡中的图像/图标而不是文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在标签
中设置图片/图标,代码如下:
在我的应用程序中我设置了标签
在底部,现在我想设置图像的文字空间..我也尝试过教程,但没有得到输出,因为我想
这里 Tab.xml
< LinearLayout xmlns:android =http://schemas.android.com/ apk / res / android
xmlns:tools =http://schemas.android.com/tools
android:layout_width =match_parent
android:layout_height =match_parent
tools:context =。MainActivity
android:orientation =vertical>
< android.support.v4.view.ViewPager
android:id =@ + id / viewpager
android:layout_width =match_parent
android:layout_height =0dp
android:layout_weight =1
android:background =@ color / white/>
< android.support.design.widget.TabLayout
android:id =@ + id / sliding_tabs
android:layout_width =match_parent
android :layout_height =wrap_content
android:background =@ color / orange
/>
< / LinearLayout>
现在 Tab.Java
package com.example.sachin.navigationwithswipe;
导入android.content.Intent;
导入android.os.Bundle;
导入android.support.annotation.Nullable;
导入android.support.design.widget.TabLayout;
导入android.support.v4.app.Fragment;
导入android.support.v4.app.FragmentManager;
导入android.support.v4.app.FragmentPagerAdapter;
导入android.support.v4.view.ViewPager;
导入android.view.LayoutInflater;
导入android.view.View;
导入android.view.ViewGroup;
导入android.widget.ImageView;
导入android.widget.TabHost;
public class TabFragment extends Fragment {
public static TabLayout tabLayout;
public static ViewPager viewPager;
public static int int_items = 3;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState){
View x = inflater.inflate(R .layout.tab_layout,NULL);
tabLayout =(TabLayout)x.findViewById(R.id.sliding_tabs);
viewPager =(ViewPager)x.findViewById(R.id.viewpager);
viewPager.setAdapter(new MyAdapter(getChildFragmentManager()));
$ b $ tabLayout.post(new Runnable(){
@Override
public void run(){
tabLayout.setupWithViewPager(viewPager);
}
});
return x;
class MyAdapter extends FragmentPagerAdapter {
$ b $ public MyAdapter(FragmentManager fm){
super(fm);
}
/ **
*就位置返回片段。
* /
@Override
public Fragment getItem(int position)
{
switch(position){
case 0:return new PrimaryFragment();
案例1:返回新的SocialFragment();
情况2:返回新的UpdatesFragment();
}
返回null;
}
@Override
public int getCount(){
return int_items;
}
/ **
*此方法根据位置返回标签的标题。
* /
@Override
public CharSequence getPageTitle(int position){
switch(position){
case 0:
返回Primary;
案例1:
返回社交;
案例2:
返回更新;
}
返回null;
}
}
}
我想使用以下代码在标签
中设置图片。使用此代码使用此代码 使用此代码在选项卡中设置图标
tabLayout.setupWithViewPager(viewPager);
//您选项卡图标
private int []图标= {
R.drawable.one,
R.drawable.two,
。 drawable.three
};
for(int i = 0; i< tabLayout.getTabCount(); i ++){
tabLayout.getTabAt(i).setIcon(icons [i]);
}
另外如果你只想要Icon,那么在Adapter上传递pageTitle null
@Override
public CharSequence getPageTitle(int position){
//返回null仅显示图标
返回null;
}
将此用于Selector。这可能工作
< selector xmlns:android =http://schemas.android.com/apk/res/android >
< item android:drawable =@ drawable / icon_onandroid:state_selected =true/>
< item android:drawable =@ drawable / icon_off/> <! - 默认 - >
< / selector>
I want to set image/icon in Tabs
in the following code:
in my application i set Tabs
in Bottom and now i want to set image in the space of the text.. i tried tutorials also but not getting output as i wanted to
Here Tab.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@color/white"/>
<android.support.design.widget.TabLayout
android:id="@+id/sliding_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/orange"
/>
</LinearLayout>
Now Tab.Java
package com.example.sachin.navigationwithswipe;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TabHost;
public class TabFragment extends Fragment {
public static TabLayout tabLayout;
public static ViewPager viewPager;
public static int int_items = 3 ;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View x = inflater.inflate(R.layout.tab_layout,null);
tabLayout = (TabLayout) x.findViewById(R.id.sliding_tabs);
viewPager = (ViewPager) x.findViewById(R.id.viewpager);
viewPager.setAdapter(new MyAdapter(getChildFragmentManager()));
tabLayout.post(new Runnable() {
@Override
public void run() {
tabLayout.setupWithViewPager(viewPager);
}
});
return x;
}
class MyAdapter extends FragmentPagerAdapter{
public MyAdapter(FragmentManager fm) {
super(fm);
}
/**
* Return fragment with respect to Position .
*/
@Override
public Fragment getItem(int position)
{
switch (position){
case 0 : return new PrimaryFragment();
case 1 : return new SocialFragment();
case 2 : return new UpdatesFragment();
}
return null;
}
@Override
public int getCount() {
return int_items;
}
/**
* This method returns the title of the tab according to the position.
*/
@Override
public CharSequence getPageTitle(int position) {
switch (position){
case 0 :
return "Primary";
case 1 :
return "Social";
case 2 :
return "Updates";
}
return null;
}
}
}
i want to set Image in the Tabs
using following code.
解决方案
Use this to set icon in tab
tabLayout.setupWithViewPager(viewPager);
//You tab icons
private int[] icons = {
R.drawable.one,
R.drawable.two,
R.drawable.three
};
for (int i = 0; i < tabLayout.getTabCount(); i++) {
tabLayout.getTabAt(i).setIcon(icons[i]);
}
Also If you want only Icon then on Adapter pass pageTitle null
@Override
public CharSequence getPageTitle(int position) {
// return null to display only the icon
return null;
}
Use this for Selector. This Might Work
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/icon_on" android:state_selected="true"/>
<item android:drawable="@drawable/icon_off"/> <!-- default -->
</selector>
这篇关于如何设置选项卡中的图像/图标而不是文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文