نوشته شده توسط
capitan_nemesis
با سلام
این دو تا عکس رو نگاه کنید مال tabhost هستش
آیکون رو نمیزاره برای tabHost فقط نوشتش هستش
مشکل از کجاست ؟
دوست عزیز!
برای نمایش تصویر دلخواه ویا حتی ویو های دلخواه در کنترل tabhost باید خودتون ویوی اختصاصی بنویسید!
این کاری هست که من برای اختصاصی کردن ویوهام استفاده کردم
public class CustomTabHost <T extends View> extends TabHost{
private Activity context;
private SparseArray<T> mTabs = new SparseArray<>();
public CustomTabHost(Context context) {
super(context);
this.context = (Activity) context;
}
public CustomTabHost(Context context, AttributeSet attrs) {
super(context, attrs);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public CustomTabHost(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public CustomTabHost(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@SuppressWarnings("unchecked")
public CustomTabHost build(SparseArray<String[]> caption) {
CustomTabHost tabs = context.findViewById(R.id.tabhost);
Resources res = context.getResources();
tabs.setup();
SparseArray<TabHost.TabSpec> tab = new SparseArray<>();
for (int i = 0; i < caption.size(); i++) {
tab.put(i, tabs.newTabSpec("tag "+String.valueOf(i)));
View tab1View = inFlateTabView(context, caption.get(i)[0],
res.getDrawable(Integer.parseInt(caption.get(i)[1])), i+1 != caption.size());
tab.get(i).setIndicator(tab1View);
if(caption.get(i)[2] != null)
tab.get(i).setContent(Integer.parseInt(caption.get (i)[2]));
tabs.addTab(tab.get(i));
mTabs.put(i,(T) tab1View);
}
parseTabView(tabs);
return tabs;
}
public View[] getTabs() {
View view[] = new View[mTabs.size()];
for (int i = 0; i < mTabs.size(); i++) {
view[i]= mTabs.get(i);
}
return view;
}
}
//run this code when tabhost is defined in Activity
private TabHost tabs ;
CustomTabHost customTabHost = new CustomTabHost(getContext());
SparseArray<String[]> caption = new SparseArray<>();
caption.put(0,new String[]{getString(R.string.x),
String.valueOf(R.mipmap.ic__gray),
String.valueOf(R.id.layout_form_x)});
caption.put(1,new String[]{getString(R.string.y),
String.valueOf(R.mipmap.ic_archive_gray),
String.valueOf(R.id.layout_y)});
caption.put(2,new String[]{getString(R.z),
String.valueOf(R.mipmap.ic_graph_gray),
String.valueOf(R.id.layout_z)});
tabs = customTabHost.build(caption);
public static View inFlateTabView(Context context, String name, Drawable viewDrawable, boolean showDivider) {
@SuppressLint("InflateParams")
View parentView = LayoutInflater.from(context).inflate(R.layout.myta b_view, null);
if (parentView != null) {
TextView tvName = parentView.findViewById(R.id.tabname);
tvName.setTypeface(FontHelperRuntime.getInstance(c ontext,StyleControl.FONT_IRAN_SANS).getTypeface()) ;
tvName.setTextSize(TypedValue.COMPLEX_UNIT_SP,VIEW _TEXT_SIZE_STYLE_2_13);
tvName.setText(name);
tvName.setTextColor(getCustomColor(false,context)) ;
ImageView iv = parentView.findViewById(R.id.tabIcon);
iv.setImageDrawable(viewDrawable);
LinearLayout divider = parentView.findViewById(R.id.divider);
divider.setVisibility(showDivider ? View.VISIBLE : View.GONE);
}
return parentView;
}
//mytab_view.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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"
android:layout_gravity="bottom"
android:background=""
android:orientation="vertical">
<ImageView
android:id="@+id/tabIcon"
android:layout_width="26dp"
android:layout_height="26dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:fitsSystemWindows="true"
tools:ignore="ContentDescription"/>
<TextView
android:id="@+id/tabname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/tabIcon"
android:layout_centerHorizontal="true"
android:layout_marginTop="4dp"
android:gravity="center"
android:theme="@style/AppTheme.AppBarOverlay"
/>
<LinearLayout
android:id="@+id/divider"
android:layout_width="1dp"
android:layout_height="wrap_content"
android:layout_alignBottom="@id/tabname"
android:layout_alignParentLeft="true"
android:layoutDirection="ltr"
>
<View
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
android:layout_marginTop="15dp"
android:background="@color/gray_variant"
/>
</LinearLayout>
</RelativeLayout>
سوالی بود مطرح کنید