2012-03-23 40 views

回答

1

你必须做出自己的布局。为了制作自定义布局并使用标准适配器(如ArrayAdapter),您必须在标准布局中指定自定义布局中的相同ID。例如,simple_list_item_checked中的TextView具有ID“@android:id/text1”(http://www.devdaily.com/java/jwarehouse/android-examples/platforms/android-2/data/res/layout/simple_list_item_checked。 xml.shtml)。在您的自定义布局中,您可以指定该ID。因此,您不必编写自定义适配器。所以最好的方法就是复制标准的xml布局并改变你想要改变的东西

+1

谢谢。当你给一个不同的id(或没有id)比标准布局中有什么可能会出错?我复制了simple_list_item_checked布局并完全删除了id,并且它在我像这样加载时似乎仍然正常工作:'ArrayAdapter adapter = new ArrayAdapter (context,R.layout.custom_checked_list,arraylist); setListAdapter(adapter);' – mattboy 2012-03-24 00:04:59

+0

是的你是对的,因为“布局”==一个TextView。但对于其他标准布局,这应该不起作用。我的意思是布局管理器中包含多个视图的布局,例如LinearLayout可能 – 207 2012-03-24 00:23:24

+1

+1您的优点。看看适配器的实现。如果适配器未使用textViewResourceId启动,则ArrayAdapter不关心id。当没有提供资源ID时,适配器假定_whole_ ressource是一个TextView。否则,适配器将搜索给定的资源ID(finViewById)(请参阅ArrayAdapter impl https://github.com/android/platform_frameworks_base/blob/master/core/java/android/widget/ArrayAdapter.java) – 207 2012-03-24 11:34:39

0

是的。这里有一个类似的事情正在完成(RES /价值/ styles.xml):

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
<style name="ContactLabelTextView"> 
    <item name="android:layout_width">wrap_content</item> 
    <item name="android:layout_height">wrap_content</item> 
    <item name="android:gravity">right</item> 
    <item name="android:textSize">14sp</item> 
    <item name="android:textColor">@android:color/white</item> 
    <item name="android:layout_marginLeft">5dp</item> 
    <item name="android:layout_marginRight">5dp</item> 
    <item name="android:layout_marginTop">5dp</item> 
    </style> 
    <style name="questionTableRow"> 
     <item name="android:layout_width">wrap_content</item> 
     <item name="android:layout_height">wrap_content</item> 
     <item name="android:layout_margin">5dp</item> 
     <item name="android:layout_marginLeft">5dp</item> 
     <item name="android:background">@drawable/textview_border</item> 
    </style> 
</resources> 

然后,当你想要的样式应用到在布局文件,您已经创建(main.xml中的元素,RES /layout/your_custom_layout.xml):

<?xml version="1.0" encoding="utf-8"?> 
    <TableRow 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/questionTableRow" 
     style="@style/questionTableRow" 

很明显,您正在使用textView来完成此操作。我提供了一个我自己的代码,其中textView获取特定样式的示例。实际上,我甚至没有应用textView样式,但是我粘贴了将样式应用于tableRow的位置,只是让您了解如何将其应用于xml元素。

这是否做你所需要的?

+0

谢谢,但我希望不必制作自己的布局文件,但以某种方式继承android.R.layout.simple_list_item_checked,然后覆盖我想更改的特定值(在本例中为文本大小),但在此示例中你从头开始创建你自己的样式。到目前为止,它看起来像你不能直接改变android.R.layout中的标准布局,你需要将它的版本写成207。 – mattboy 2012-03-24 12:07:18

相关问题