【问题标题】:How to change color and font on ListView如何在 ListView 上更改颜色和字体
【发布时间】:2011-11-13 17:43:16
【问题描述】:

我正在尝试更改我的字体(颜色和大小)以及我的 ListView 的背景。我想用不在 xml 上的代码行来改变它。 我的列表视图如下所示: xml:

 <?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="18sp" android:text="@string/hello">
</TextView>

我的代码是

public class NewsActivity  extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);

 // ArrayAdapter listItemAdapter = new ArrayAdapter( this,android.R.layout.simple_list_item_1, v_itemList );

      setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item,ynetList));

      View v=getListView() ;

      ListView lv = getListView();

接下来呢?请根据我的代码给我一个示例

【问题讨论】:

  • 您是否尝试在创建列表时设置颜色和大小?还是在创建列表后对特定的孩子这样做?
  • 我想在创建之前更改颜色和大小。我 int childCount = lv.getChildCount();但得到 0.how to fix it

标签: android listview fonts


【解决方案1】:

您需要创建一个 CustomListAdapter。

public class CustomListAdapter extends ArrayAdapter <String> {

    private Context mContext;
    private int id;
    private List <String>items ;

    public CustomListAdapter(Context context, int textViewResourceId , List<String> list ) 
    {
        super(context, textViewResourceId, list);           
        mContext = context;
        id = textViewResourceId;
        items = list ;
    }

    @Override
    public View getView(int position, View v, ViewGroup parent)
    {
        View mView = v ;
        if(mView == null){
            LayoutInflater vi = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            mView = vi.inflate(id, null);
        }

        TextView text = (TextView) mView.findViewById(R.id.textView);

        if(items.get(position) != null )
        {
            text.setTextColor(Color.WHITE);
            text.setText(items.get(position));
            text.setBackgroundColor(Color.RED); 
            int color = Color.argb( 200, 255, 64, 64 );
                text.setBackgroundColor( color );

        }

        return mView;
    }

}

列表项如下所示(custom_list.xml):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/textView"
    android:textSize="20px" android:paddingTop="10dip" android:paddingBottom="10dip"/>
</LinearLayout>

使用 TextView api 来根据自己的喜好装饰文本

你会像这样使用它

listAdapter = new CustomListAdapter(YourActivity.this , R.layout.custom_list , mList);
mListView.setAdapter(listAdapter);

【讨论】:

  • 我试过你在这里给出的代码......它工作得很好......我想要自定义的一件事是如果位置等于 0,那么我只需要更改那个 textview 的颜色。它有效,但如果我滚动建议,其他文本视图颜色也会改变。我该怎么做才能只设置位置 1 的 textview 的颜色?
  • 如果你给这个适配器添加泛型,效果会更好
【解决方案2】:

创建一个 CustomAdapter 并且你有 getView() 所以如果你想改变 listview 背景颜色使用这个:

v.setBackgroundColor(Color.CYAN);

如果你想改变 textColor 然后这样做:

tv.setTextColor(Color.RED);

对于 textSize :

tv.setTextSize(20);

'v' 是列表视图,'tv' 是文本视图

【讨论】:

    【解决方案3】:

    更好的是,您不需要为列表单元格视图创建单独的 android xml 布局。如果列表仅包含 textview,则可以只使用“android.R.layout.simple_list_item_1”。

    private class ExampleAdapter extends ArrayAdapter<String>{
    
        public ExampleAdapter(Context context, int textViewResourceId, String[] objects) {
            super(context, textViewResourceId, objects);
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
    
    
            View view =  super.getView(position, convertView, parent);
    
            TextView tv = (TextView) view.findViewById(android.R.id.text1);
            tv.setTextColor(0);
    
            return view;
        }
    

    【讨论】:

      【解决方案4】:

      您可以选择喜欢的孩子

      TextView tv = (TextView)lv.getChildAt(0);
      tv.setTextColor(Color.RED);
      tv.setTextSize(12);    
      

      【讨论】:

      • 试过了,但在 tv = (TextView)lv.getChildAt(i); 上得到了 null;
      【解决方案5】:

      如果你想设置列表的背景,那么将图像放在

      之前
      < ImageView
      android:background="@drawable/image_name"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"/>
      

      如果你想改变颜色然后把颜色代码放在上面的文本框上

       android:textColor="#ffffff"
      

      【讨论】:

        【解决方案6】:

        如果您只需要更改视图的一些参数和 ArrayAdapter 的默认行为,您可以:

         import android.content.Context;
         import android.view.View;
         import android.view.ViewGroup;
         import android.widget.ArrayAdapter;
        
         public class CustomArrayAdapter<T> extends ArrayAdapter<T> {
        
            public CustomArrayAdapter(Context context, int textViewResourceId) {
                super(context, textViewResourceId);
            }
        
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View view = super.getView(position, convertView, parent);
        
                    // Here all your customization on the View
                    view.setBackgroundColor(.......);
                    ...
        
                return view;
            }
        
        
         }
        

        【讨论】:

          【解决方案7】:

          如果您想使用 colors.xml 中的颜色,请进行实验 :

             public View getView(int position, View convertView, ViewGroup parent) {
                  ... 
                  View rowView = inflater.inflate(this.rowLayoutID, parent, false);
                  rowView.setBackgroundColor(rowView.getResources().getColor(R.color.my_bg_color));
                  TextView title = (TextView) rowView.findViewById(R.id.txtRowTitle);
                  title.setTextColor(
                      rowView.getResources().getColor(R.color.my_title_color));
                  ...
               }
          

          你也可以使用:

          private static final int bgColor = 0xAAAAFFFF;
          public View getView(int position, View convertView, ViewGroup parent) {
                  ... 
                  View rowView = inflater.inflate(this.rowLayoutID, parent, false);
                      rowView.setBackgroundColor(bgColor);
          ...
          }
          

          【讨论】:

            【解决方案8】:

            像这样在 Java 代码中使用它们:

             color = getResources().getColor(R.color.mycolor);
            

            getResources() 方法返回当前活动的 ResourceManager 类,getColor() 要求管理器查找给定资源 ID 的颜色

            【讨论】:

              【解决方案9】:

              在 android 6.0 中,您可以更改文本的颜色,如下所示

              holder._linear_text_active_release_pass.setBackgroundColor(ContextCompat.getColor(context, R.color.green));
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2013-09-24
                • 1970-01-01
                • 2018-11-19
                • 1970-01-01
                • 1970-01-01
                • 2020-12-09
                • 1970-01-01
                相关资源
                最近更新 更多