我是 android 的新手,正在为 ListView 使用自定义字体。我不知道如何在 ListView 中使用字体。我也尝试了不同的示例,但无法解决我的问题。这是我的代码
public class HomeScreen extends ListActivity {
private static final int QUICK_START_INDEX =0;
private static final int CUSTOM = 1;
private static final int CALL_LIST = 2;
private static final int CALENDAR = 3;
private static final int TEMPLATES=4;
private static final int USER_GUIDE = 5;
static final String[] LIST =
new String[] { "QuickStart", "Custom", "CallList", "Calendar","Templates","UserGuide"};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new MobileArrayAdapter(this, LIST));
/*Typeface font = Typeface.createFromAsset(getAssets(), "earthkid.ttf"); */
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
//get selected items
String selectedValue = (String) getListAdapter().getItem(position);
/*Toast.makeText(this, selectedValue, Toast.LENGTH_SHORT).show();*/
if(position==QUICK_START_INDEX){
startActivity(new Intent(HomeScreen.this,ConfDialer.class));
}
}
}
移动阵列适配器.java
public class MobileArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] values;
public MobileArrayAdapter(Context context, String[] values) {
super(context, R.layout.list_home, values);
this.context = context;
this.values = values;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.list_home, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.label);
ImageView imageView = (ImageView) rowView.findViewById(R.id.logo);
textView.setText(values[position]);
// Change icon based on name
String s = values[position];
System.out.println(s);
if (s.equals("QuickStart")) {
imageView.setImageResource(R.drawable.quick_strat);
} else if (s.equals("Custom")) {
imageView.setImageResource(R.drawable.customize);
} else if (s.equals("CallList")) {
imageView.setImageResource(R.drawable.call_list);
} else if(s.equals("Calendar")){
imageView.setImageResource(R.drawable.calendar);
} else if(s.equals("Templates")){
imageView.setImageResource(R.drawable.templates);
}
else{
imageView.setImageResource(R.drawable.user_guide);
}
return rowView;
}
}
谁能为此提供解决方案。
最佳答案
试试这个...
您可以在 Activity 中创建适配器时覆盖 getView(),例如具有 name 和 title 的 SimpleAdapter。
//global declaration
ListAdapter adapter;
adapter = SimpleAdapter(getActivity(), your_list, R.layout.your_list_items, new String[]{"Name", "Title"}, new int[]{R.id.name, R.id.title}){
@Override
public View getView(int position, View convertView, ViewGroup parent) {
v = convertView;
if(v == null){
LayoutInflater li = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = li.inflate(R.layout.interview_list_items, null);
TextView titleText= (TextView) v.findViewById(R.id.title);
TextView nameText= (TextView) v.findViewById(R.id.name);
childFont = Typeface.createFromAsset(getActivity().getAssets(),"fonts/Roboto-Light.ttf");
titleText.setTypeface(childFont);
nameText.setTypeface(childFont);
return super.getView(position, v, parent);
}
};
或者像这样在 Adapter 类中尝试...
public class MobileArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] values;
private Typeface tf;
public MobileArrayAdapter(Context context, String[] values) {
super(context, R.layout.list_home, values);
this.context = context;
this.values = values;
this.tf = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Light.ttf"); //initialize typeface here.
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.list_home, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.label);
ImageView imageView = (ImageView) rowView.findViewById(R.id.logo);
textView.setTypeface(tf); // set typeface here
textView.setText(values[position]);
// Change icon based on name
String s = values[position];
System.out.println(s);
if (s.equals("QuickStart")) {
imageView.setImageResource(R.drawable.quick_strat);
} else if (s.equals("Custom")) {
imageView.setImageResource(R.drawable.customize);
} else if (s.equals("CallList")) {
imageView.setImageResource(R.drawable.call_list);
} else if(s.equals("Calendar")){
imageView.setImageResource(R.drawable.calendar);
} else if(s.equals("Templates")){
imageView.setImageResource(R.drawable.templates);
}
else{
imageView.setImageResource(R.drawable.user_guide);
}
return rowView;
}
应用目录结构
关于android - 如何为android listview设置自定义字体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18734273/
我有一个Ruby程序,它使用rubyzip压缩XML文件的目录树。gem。我的问题是文件开始变得很重,我想提高压缩级别,因为压缩时间不是问题。我在rubyzipdocumentation中找不到一种为创建的ZIP文件指定压缩级别的方法。有人知道如何更改此设置吗?是否有另一个允许指定压缩级别的Ruby库? 最佳答案 这是我通过查看rubyzip内部创建的代码。level=Zlib::BEST_COMPRESSIONZip::ZipOutputStream.open(zip_file)do|zip|Dir.glob("**/*")d
我正在尝试设置一个puppet节点,但rubygems似乎不正常。如果我通过它自己的二进制文件(/usr/lib/ruby/gems/1.8/gems/facter-1.5.8/bin/facter)在cli上运行facter,它工作正常,但如果我通过由rubygems(/usr/bin/facter)安装的二进制文件,它抛出:/usr/lib/ruby/1.8/facter/uptime.rb:11:undefinedmethod`get_uptime'forFacter::Util::Uptime:Module(NoMethodError)from/usr/lib/ruby
我在使用omniauth/openid时遇到了一些麻烦。在尝试进行身份验证时,我在日志中发现了这一点:OpenID::FetchingError:Errorfetchinghttps://www.google.com/accounts/o8/.well-known/host-meta?hd=profiles.google.com%2Fmy_username:undefinedmethod`io'fornil:NilClass重要的是undefinedmethodio'fornil:NilClass来自openid/fetchers.rb,在下面的代码片段中:moduleNetclass
我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer
我刚刚为fedora安装了emacs。我想用emacs编写ruby。为ruby提供代码提示、代码完成类型功能所需的工具、扩展是什么? 最佳答案 ruby-mode已经包含在Emacs23之后的版本中。不过,它也可以通过ELPA获得。您可能感兴趣的其他一些事情是集成RVM、feature-mode(Cucumber)、rspec-mode、ruby-electric、inf-ruby、rinari(用于Rails)等。这是我当前用于Ruby开发的Emacs配置:https://github.com/citizen428/emacs
我正在查看instance_variable_set的文档并看到给出的示例代码是这样做的:obj.instance_variable_set(:@instnc_var,"valuefortheinstancevariable")然后允许您在类的任何实例方法中以@instnc_var的形式访问该变量。我想知道为什么在@instnc_var之前需要一个冒号:。冒号有什么作用? 最佳答案 我的第一直觉是告诉你不要使用instance_variable_set除非你真的知道你用它做什么。它本质上是一种元编程工具或绕过实例变量可见性的黑客攻击
我想向我的Controller传递一个参数,它是一个简单的复选框,但我不知道如何在模型的form_for中引入它,这是我的观点:{:id=>'go_finance'}do|f|%>Transferirde:para:Entrada:"input",:placeholder=>"Quantofoiganho?"%>Saída:"output",:placeholder=>"Quantofoigasto?"%>Nota:我想做一个额外的复选框,但我该怎么做,模型中没有一个对象,而是一个要检查的对象,以便在Controller中创建一个ifelse,如果没有检查,请帮助我,非常感谢,谢谢
我已经从我的命令行中获得了一切,所以我可以运行rubymyfile并且它可以正常工作。但是当我尝试从sublime中运行它时,我得到了undefinedmethod`require_relative'formain:Object有人知道我的sublime设置中缺少什么吗?我正在使用OSX并安装了rvm。 最佳答案 或者,您可以只使用“require”,它应该可以正常工作。我认为“require_relative”仅适用于ruby1.9+ 关于ruby-主要:Objectwhenrun
我想设置一个默认日期,例如实际日期,我该如何设置?还有如何在组合框中设置默认值顺便问一下,date_field_tag和date_field之间有什么区别? 最佳答案 试试这个:将默认日期作为第二个参数传递。youcorrectlysetthedefaultvalueofcomboboxasshowninyourquestion. 关于ruby-on-rails-date_field_tag,如何设置默认日期?[rails上的ruby],我们在StackOverflow上找到一个类似的问
我有一些代码在几个不同的位置之一运行:作为具有调试输出的命令行工具,作为不接受任何输出的更大程序的一部分,以及在Rails环境中。有时我需要根据代码的位置对代码进行细微的更改,我意识到以下样式似乎可行:print"Testingnestedfunctionsdefined\n"CLI=trueifCLIdeftest_printprint"CommandLineVersion\n"endelsedeftest_printprint"ReleaseVersion\n"endendtest_print()这导致:TestingnestedfunctionsdefinedCommandLin