Android 更改 listSeparatorTextViewStyle 中使用的线条颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12171209/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:58:11  来源:igfitidea点击:

Change the line color used in listSeparatorTextViewStyle

android

提问by Cheok Yan Cheng

I have the following code

我有以下代码

        <TextView
            android:text="@string/hello"
            style="?android:attr/listSeparatorTextViewStyle" />

and I will get the following effect.

我会得到以下效果。

enter image description here

在此处输入图片说明

However, I am not happy with the color line. I would like to have something like

但是,我对颜色线不满意。我想要类似的东西

enter image description here

在此处输入图片说明

I would like it to have blue color line as in holo. I try the following custom style.

我希望它像全息一样具有蓝色线条。我尝试以下自定义样式。

<style name="MyOwnListSeperatorTextViewStyle">        
<item name="android:background">@android:drawable/list_section_divider_holo_light</item>        
<item name="android:textAllCaps">true</item>    

<!-- Copy from Widget.TextView.ListSeparator -->

<item name="android:background">@android:drawable/dark_header_dither</item>
    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:textStyle">bold</item>
    <item name="android:textColor">?textColorSecondary</item>
    <item name="android:textSize">14sp</item>
    <item name="android:gravity">center_vertical</item>
    <item name="android:paddingLeft">8dip</item>        
</style>

But it won't work, as I get the following error.

但它不起作用,因为我收到以下错误。

error: Error: Resource is not public. (at 'android:background' with value '@android:drawable/dark_header_dither').

错误:错误:资源不公开。(在 'android:background' 中,值为 '@android:drawable/dark_header_dither')。

Have idea how can I change the line color used in listSeparatorTextViewStyle?

知道如何更改 中使用的线条颜色listSeparatorTextViewStyle吗?

回答by Daniel Smith

I needed to do this to override the typical Holo Spinner style (I didn't want the underlined item - i just wanted the arrow), and I think this can be overridden in precisely the same manner:

我需要这样做来覆盖典型的 Holo Spinner 样式(我不想要带下划线的项目 - 我只想要箭头),我认为这可以以完全相同的方式覆盖:

First off, you want to find the item you wish to override in the android styles source. There is an incredibly useful SO answer that contains all of the styles (and the names to override them) right here: Set Dialog theme to parent Theme in Android

首先,您要在 android 样式源中找到要覆盖的项目。这里有一个非常有用的 SO 答案,其中包含所有样式(以及覆盖它们的名称):Set Dialog theme to parent Theme in Android

I believe yours is the following line:

我相信你的是以下几行:

<item name="listSeparatorTextViewStyle">@android:style/Widget.Holo.Light.TextView.ListSeparator</item>

This takes us on a journey to find the style Widget.Holo.Light.TextView.ListSeparator which should live somewhere on your very own computer! But I'll make it easy and just c&p it:

这将带我们去寻找 Widget.Holo.Light.TextView.ListSeparator 样式,它应该存在于您自己的计算机上!但我会让它变得简单,只是 c&p 它:

<style name="Widget.Holo.Light.TextView.ListSeparator" parent="Widget.TextView.ListSeparator">
    <item name="android:background">@android:drawable/list_section_divider_holo_light</item>
</style>

Now, you probably want to leave well enough alone, and justlook at that background drawable. You will find it is a grey 9patch file that looks like the sinister grey line you seek to avoid.

现在,你可能想离开不够孤独,只是看那个背景绘制。你会发现它是一个灰色的 9patch 文件,看起来像你试图避免的阴险的灰线。

We need to override this. I am sure there are a number of ways to do this, but I do so by customizing the theme of the application. Here is the themes.xml file:

我们需要覆盖它。我确信有很多方法可以做到这一点,但我是通过自定义应用程序的主题来实现的。这是 themes.xml 文件:

<style name="AppTheme" parent="@android:style/Theme.Holo.Light.NoActionBar">
    <item name="android:listSeparatorTextViewStyle">@style/MyOwnListSeperatorTextViewStyle</item>
</style>

<style name="MyOwnListSeperatorTextViewStyle" parent="Widget.TextView.ListSeparator">
    <item name="android:background">@drawable/make_your_own_blue_9_patch_here</item>
</style>

Notice how we used the listSeparatorTextViewStylefrom that previous SO post? And the parent of the custom style is the Widget.TextView.ListSeparatorfrom android's style source? All very important.

请注意我们如何使用listSeparatorTextViewStyle之前的 SO 帖子中的 ?自定义样式的父项Widget.TextView.ListSeparator来自android的样式源?都非常重要。

Now you just need to apply this theme to your app, but I am assuming you have a theme already. If you haven't already, you will need to make your own 9patchbut I would just look at the list_section_divider_holo_light.9.png file on your computer, and make the grey parts blue, and then make a copy and place it into your own drawables folder.

现在您只需要将此主题应用于您的应用程序,但我假设您已经有一个主题。如果你还没有,你需要制作你自己的 9patch但我只是看看你电脑上的 list_section_divider_holo_light.9.png 文件,把灰色的部分变成蓝色,然后复制一份放到你自己的可绘制文件夹。

Hope this works and is helpful!

希望这有效并且有帮助!