【问题标题】:Can simple_list_item_2 be customized?simple_list_item_2 可以自定义吗?
【发布时间】:2015-02-19 10:57:22
【问题描述】:

我在代码中使用了 simple_list_item_2 来创建列表视图。一切正常,但我想更改布局的背景颜色。可以吗?这种布局也可以像用户定义的 XML 一样自定义吗?

【问题讨论】:

  • IMO 你不能,但你可以用相同的代码创建另一个布局,然后替换 simple_list_item_2 的背景,然后用你想要的颜色更改它,然后调用你的 new 而不是调用 simple_list_item_2布局,它会工作。
  • 只需提供您的自定义行布局。
  • 感谢您的回复。我正在创建另一个布局
  • @user232803 查看我的编辑:P
  • stackoverflow.com/questions/16062569/… 这可能会有所帮助

标签: android android-layout android-listview customization


【解决方案1】:

像这样创建自己的simple_list_item_2,然后使用它

<TwoLineListItem xmlns:android="http://schemas.android.com/apk/res/android"
   <!-- This is your background color-->
   android:background="#DC143C"

   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:minHeight="?android:attr/listPreferredItemHeight"
   android:mode="twoLine"
   android:paddingStart="?android:attr/listPreferredItemPaddingStart"
   android:paddingEnd="?android:attr/listPreferredItemPaddingEnd">
      <TextView android:id="@android:id/text1"
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:layout_marginTop="8dip"
             android:textAppearance="?android:attr/textAppearanceListItem" />
      <TextView android:id="@android:id/text2"
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:layout_below="@android:id/text1"
             android:layout_alignStart="@android:id/text1"
             android:textAppearance="?android:attr/textAppearanceListItemSecondary" />
 </TwoLineListItem>

(取自android source code

【讨论】:

  • 谢谢!我正在根据您的回答创建新布局
  • 我在这一行出现错误,因为我当前的最小值是 11。还有什么可以替代这个吗? android:textAppearance="?android:attr/textAppearanceListItem"
【解决方案2】:

IMO 你不能,但你可以使用相同的代码创建另一个布局,然后替换 simple_list_item_2 的背景,然后用你想要的颜色更改它,然后调用你的新布局而不是调用 simple_list_item_2它会起作用的。

编辑*

按照@JonasCz 的回答,您可以对其进行编辑,替换它的示例代码如下:

ListView l = (ListView) findViewById(R.id.listview);
String[] values = new String[] { "Ubuntu", "Android", "iPhone",
    "Windows", "Ubuntu", "Android", "iPhone", "Windows" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
    R.layout.your_custom_simple_list_item_2, values); //removed the android. 
viewContainer = findViewById(R.id.undobar);
l.setAdapter(adapter);

代码来自AndroidListView/article.html

【讨论】:

    猜你喜欢
    • 2012-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多