我有一个自定义列表视图,我无法弄清楚如何更改listitems文本颜色,样式,大小等我已尝试在hte xml文件中编辑,但它没有帮助。我是Android的新手,任何帮助都将受到赞赏..
**layout**
********
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:background="@drawable/background"
android:layout_height="wrap_content"
>
<EditText
android:id="@+id/inputSearch"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:hint="Search"
android:drawableRight="@drawable/search"
android:inputType="textVisiblePassword"
android:cacheColorHint="#00000000"
android:background="#D8D8D8"
>
</EditText>
<ListView
android:id="@+id/ListView01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:dividerHeight="1dp"
android:divider="#2D9E00"
android:listSelector="@drawable/list_selector"
android:cacheColorHint="#00000000"
android:fastScrollEnabled="true">
</ListView>
</LinearLayout>
**list_selector.xml**
*******************
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_selected="false"
android:state_pressed="false"
android:drawable="@drawable/gradient_bg" />
<item android:state_pressed="true"
android:drawable="@drawable/gradient_bg_hover" />
<item android:state_selected="true"
android:state_pressed="false"
android:drawable="@drawable/gradient_bg_hover" />
</selector>
**gradient_bg.xml
*********************
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#2D9E00"
android:centerColor="#2D9E00"
android:endColor="#2D9E00"
android:angle="270" />
</shape>
**gradient_bg_hover.xml**
**************************
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#2D9E00"
android:centerColor="#2D9E00"
android:endColor="#2D9E00"
android:angle="270" />
</shape>
我的java代码
public class MainActivity extends Activity {
private ListView list1;
private String array[] ;
EditText inputSearch;
ArrayAdapter<String> adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
inputSearch = (EditText) findViewById(R.id.inputSearch);
list1 = (ListView) findViewById(R.id.ListView01);
array = getResources().getStringArray(R.array.myArray);
adapter =new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, getResources().getStringArray(R.array.myArray));
list1.setAdapter(adapter);
inputSearch.addTextChangedListener(new TextWatcher() {
答案 0 :(得分:0)
您使用android.R.layout.simple_list_item_1
作为ListView
中项目的布局。如果要自定义项目的布局,请按照下列步骤操作:
步骤1)创建自定义XML布局(simple_list_item_custom.xml
),如下所示:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@android:id/text1"
style="?android:attr/listItemFirstLineStyle"
android:paddingTop="2dip"
android:paddingBottom="3dip"
android:textColor="#FF0000"/>
在上面的布局中,添加XML属性以更改字体大小,颜色等。(就像我添加android:textColor="#FF0000"
一样,使颜色变红。
步骤2)使用自定义布局初始化适配器:
adapter =new ArrayAdapter<String>(this, R.layout.simple_list_item_custom, getResources().getStringArray(R.array.myArray));
其他一切都保持不变..