android 自定义indicator,Android中的“自定义”标签-使用“ setIndicator”时出现错误...

我正在尝试在我的android应用中创建自定义标签,我得到了一些示例代码,但是该代码在setIndicator方法中显示错误…这是我的代码

我遇到的错误是-类型TabHost.TabSpec中的setIndicator(CharSequence)方法不适用于自变量(TextView)

package com.myapp;

import android.app.Activity;

import android.graphics.Color;

import android.os.Bundle;

import android.view.Gravity;

import android.view.View;

import android.widget.FrameLayout;

import android.widget.LinearLayout;

import android.widget.TabHost;

import android.widget.TabWidget;

import android.widget.TextView;

import android.widget.LinearLayout.LayoutParams;

import android.widget.TabHost.TabContentFactory;

import android.widget.TabHost.TabSpec;

//Custom Tabs

public class MyActivity extends Activity {

int tabHeight = 40;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

LinearLayout main = new LinearLayout(this);

main.setOrientation(LinearLayout.VERTICAL);

setContentView(main);

TabHost tabs = new TabHost(this);

tabs.setId(android.R.id.tabhost);

main.addView(tabs);

TabWidget tabWidget = new TabWidget(this);

tabWidget.setId(android.R.id.tabs);

tabs.addView(tabWidget);

FrameLayout tabContent = new FrameLayout(this);

tabContent.setId(android.R.id.tabcontent);

tabContent.setPadding(0, tabHeight, 0, 0);

tabs.addView(tabContent);

TextView content = new TextView(this);

content.setText("This is the Frame Content");

content.setId(100);

tabs.setup();

TabSpec tspec1 = tabs.newTabSpec("Tab1");

tspec1.setIndicator(makeTabIndicator("One"));

tspec1.setContent(new PreExistingViewFactory(content));

tabs.addTab(tspec1);

TabSpec tspec2 = tabs.newTabSpec("Tab2");

tspec2.setIndicator(makeTabIndicator("Two"));

tspec2.setContent(new PreExistingViewFactory(content));

tabs.addTab(tspec2);

TabSpec tspec3 = tabs.newTabSpec("Tab3");

tspec3.setIndicator(makeTabIndicator("Three"));

tspec3.setContent(new PreExistingViewFactory(content));

tabs.addTab(tspec3);

}

private TextView makeTabIndicator(String text){

TextView tabView = new TextView(this);

LayoutParams lp3 = new LayoutParams(LayoutParams.WRAP_CONTENT, tabHeight, 1);

lp3.setMargins(1, 0, 1, 0);

tabView.setLayoutParams(lp3);

tabView.setText(text);

tabView.setTextColor(Color.WHITE);

tabView.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL);

tabView.setBackgroundDrawable( getResources().getDrawable(R.drawable.tab_indicator));

tabView.setPadding(13, 0, 13, 0);

return tabView;

}

class PreExistingViewFactory implements TabContentFactory{

private final View preExisting;

protected PreExistingViewFactory(View view){

preExisting = view;

}

public View createTabContent(String tag) {

return preExisting;

}

}

}

任何人都可以帮助我解决此问题…

提前谢谢..

克里斯

解决方法:

不幸的是,方法setIndicator(View view)仅适用于Android 1.6及更高版本(版本4).如果您支持Android 1.5(版本3),则只能使用setIndicator(CharSequence标签)将String / CharSequence用作指标.

请注意,方法setIndicator(View view)为“ Since:API Level 4”

标签:tabs,android

来源: https://codeday.me/bug/20191208/2093682.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值