如何添加填充只是列表视图的默认分隔符?

时间:2013-02-20 17:28:59

标签: android android-listview

我想在我的listview分隔符中添加左右填充,因为每行应该在屏幕的最左侧有一个纯色,如果我在整个列表中添加填充,那么这些颜色也会被填充。我知道我可以制作自己的分频器,但我无法找到默认分频器的可画面。

我试图像这样做一个可绘制的:

    <View xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:background="?android:attr/listDivider" />

然后将其作为分隔符:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/background_pattern" >

<ListView
    android:id="@+id/status_list"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:divider="@drawable/list_divider"
    android:headerDividersEnabled="false"
    android:footerDividersEnabled="false" >
</ListView>

但应用程序部队以此结束:

02-20 18:02:15.374: E/AndroidRuntime(13457): FATAL EXCEPTION: main
02-20 18:02:15.374: E/AndroidRuntime(13457): android.view.InflateException: Binary XML file line #7: Error inflating class android.widget.ListView
02-20 18:02:15.374: E/AndroidRuntime(13457):    at android.view.LayoutInflater.createView(LayoutInflater.java:613)
02-20 18:02:15.374: E/AndroidRuntime(13457):    at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
02-20 18:02:15.374: E/AndroidRuntime(13457):    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:660)
02-20 18:02:15.374: E/AndroidRuntime(13457):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
02-20 18:02:15.374: E/AndroidRuntime(13457):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
02-20 18:02:15.374: E/AndroidRuntime(13457):    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
02-20 18:02:15.374: E/AndroidRuntime(13457):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
02-20 18:02:15.374: E/AndroidRuntime(13457):    at com.bitnation.tubeappy.StatusFragment.onCreateView(StatusFragment.java:79)
02-20 18:02:15.374: E/AndroidRuntime(13457):    at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
02-20 18:02:15.374: E/AndroidRuntime(13457):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
02-20 18:02:15.374: E/AndroidRuntime(13457):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
02-20 18:02:15.374: E/AndroidRuntime(13457):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
02-20 18:02:15.374: E/AndroidRuntime(13457):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
02-20 18:02:15.374: E/AndroidRuntime(13457):    at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:461)
02-20 18:02:15.374: E/AndroidRuntime(13457):    at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
02-20 18:02:15.374: E/AndroidRuntime(13457):    at android.support.v4.view.ViewPager.populate(ViewPager.java:1012)
02-20 18:02:15.374: E/AndroidRuntime(13457):    at android.support.v4.view.ViewPager.populate(ViewPager.java:881)
02-20 18:02:15.374: E/AndroidRuntime(13457):    at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1366)
02-20 18:02:15.374: E/AndroidRuntime(13457):    at android.view.View.measure(View.java:15518)
02-20 18:02:15.374: E/AndroidRuntime(13457):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
02-20 18:02:15.374: E/AndroidRuntime(13457):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
02-20 18:02:15.374: E/AndroidRuntime(13457):    at android.view.View.measure(View.java:15518)
02-20 18:02:15.374: E/AndroidRuntime(13457):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
02-20 18:02:15.374: E/AndroidRuntime(13457):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
02-20 18:02:15.374: E/AndroidRuntime(13457):    at android.view.View.measure(View.java:15518)
02-20 18:02:15.374: E/AndroidRuntime(13457):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
02-20 18:02:15.374: E/AndroidRuntime(13457):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
02-20 18:02:15.374: E/AndroidRuntime(13457):    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2176)
02-20 18:02:15.374: E/AndroidRuntime(13457):    at android.view.View.measure(View.java:15518)
02-20 18:02:15.374: E/AndroidRuntime(13457):    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1874)
02-20 18:02:15.374: E/AndroidRuntime(13457):    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089)
02-20 18:02:15.374: E/AndroidRuntime(13457):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265)
02-20 18:02:15.374: E/AndroidRuntime(13457):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
02-20 18:02:15.374: E/AndroidRuntime(13457):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
02-20 18:02:15.374: E/AndroidRuntime(13457):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
02-20 18:02:15.374: E/AndroidRuntime(13457):    at android.view.Choreographer.doCallbacks(Choreographer.java:562)
02-20 18:02:15.374: E/AndroidRuntime(13457):    at android.view.Choreographer.doFrame(Choreographer.java:532)
02-20 18:02:15.374: E/AndroidRuntime(13457):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
02-20 18:02:15.374: E/AndroidRuntime(13457):    at android.os.Handler.handleCallback(Handler.java:725)
02-20 18:02:15.374: E/AndroidRuntime(13457):    at android.os.Handler.dispatchMessage(Handler.java:92)
02-20 18:02:15.374: E/AndroidRuntime(13457):    at android.os.Looper.loop(Looper.java:137)
02-20 18:02:15.374: E/AndroidRuntime(13457):    at android.app.ActivityThread.main(ActivityThread.java:5041)
02-20 18:02:15.374: E/AndroidRuntime(13457):    at java.lang.reflect.Method.invokeNative(Native Method)
02-20 18:02:15.374: E/AndroidRuntime(13457):    at java.lang.reflect.Method.invoke(Method.java:511)
02-20 18:02:15.374: E/AndroidRuntime(13457):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-20 18:02:15.374: E/AndroidRuntime(13457):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-20 18:02:15.374: E/AndroidRuntime(13457):    at dalvik.system.NativeStart.main(Native Method)
02-20 18:02:15.374: E/AndroidRuntime(13457): Caused by: java.lang.reflect.InvocationTargetException
02-20 18:02:15.374: E/AndroidRuntime(13457):    at java.lang.reflect.Constructor.constructNative(Native Method)
02-20 18:02:15.374: E/AndroidRuntime(13457):    at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
02-20 18:02:15.374: E/AndroidRuntime(13457):    at android.view.LayoutInflater.createView(LayoutInflater.java:587)
02-20 18:02:15.374: E/AndroidRuntime(13457):    ... 46 more
02-20 18:02:15.374: E/AndroidRuntime(13457): Caused by: android.content.res.Resources$NotFoundException: File res/drawable/list_divider.xml from drawable resource ID #0x7f020007
02-20 18:02:15.374: E/AndroidRuntime(13457):    at android.content.res.Resources.loadDrawable(Resources.java:1953)
02-20 18:02:15.374: E/AndroidRuntime(13457):    at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
02-20 18:02:15.374: E/AndroidRuntime(13457):    at android.widget.ListView.(ListView.java:155)
02-20 18:02:15.374: E/AndroidRuntime(13457):    at android.widget.ListView.(ListView.java:139)
02-20 18:02:15.374: E/AndroidRuntime(13457):    ... 49 more

1 个答案:

答案 0 :(得分:2)

以下是问题,android:divider需要@drawable@color(请参阅docs),但您在@layout下保存了@drawable {1}}文件夹(这就是它编译的原因),但在运行时崩溃。

android.content.res.Resources$NotFoundException: File res/drawable/list_divider.xml from drawable resource ID #0x7f020007

不幸的是,我认为没有任何好方法可以引用默认的android drawable list divider AND 同时覆盖其填充/边界。如果你得到了drawable的源文件,你可以编辑9.png文件的可伸缩/内容区域来调整它的填充,但这是相当繁琐的。

这是我找到的解决方法(并且运行正常):设置android:divider="@null",并将分隔符View添加到row_layout.xml的底部(用于绑定到ListView)。基本上你会伪造分频器,它会在每一行内而不是在它们之间,但对于用户来说它看起来是一样的。然后,您将能够控制分隔线的填充与每行的填充分开,这是您的目标。