简体   繁体   English

Android:在自定义按钮中使用默认的突出显示颜色

[英]Android: Use default highlight colors in a custom button

I want to use a Button in my android app but I want to customize how it looks. 我想在我的Android应用程序中使用Button,但是我想自定义它的外观。 However, I want the highlight and selected colors of the button to be the same as the default colors (ie the dark and light orange gradients, or whatever the theme color supplies). 但是,我希望按钮的突出显示和所选颜色与默认颜色相同(即,深橙色和浅橙色渐变,或主题颜色提供的任何颜色)。

Is there anyway to get the default highlight/selected drawables and to use that as the fill for my buttons on the selected and highlighted states? 无论如何,有没有获取默认的突出显示/选定的可绘制对象并将其用作我在选定和突出显示的状态上的按钮的填充?

Thanks! 谢谢!

It seems that you can use a selector as drawable inside a selector! 看来您可以在选择器内部将选择器用作可绘制对象! (You can or should not use @android:drawable/btn_default_selected , because it is private) This meens that you can write your own selecter and use the whole default android selector for the items you want the default behavior for. (您可以或不应该使用@android:drawable/btn_default_selected ,因为它是私有的)。这意味着您可以编写自己的选择器,并为想要默认行为的项使用整个默认android选择器。

I used this selector 我用了这个选择器

<selector xmlns:android="http://schemas.android.com/apk/res/android" >

    <item android:drawable="@android:drawable/btn_default" android:state_pressed="true"/>

</selector>

And added it to as background to a linear layout. 并将其添加为线性布局的背景。 I don't know why, but this messed up the padding/margin as well, thats why i set them to 0. 我不知道为什么,但是这也弄乱了填充/边距,这就是为什么我将它们设置为0。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/background_linear_layout_button"
    android:padding="0dp"
    android:layout_margin="0dp"
    android:orientation="vertical" >

    <!-- YOUR LAYOUT THAT ACTS LIKE A BUTTON -->
</LinearLayout>

The Result is that you have the parent background color in the unpressed state and the android background color for the pressed state. 结果是您具有处于未按下状态的父背景颜色和处于已按下状态的android背景颜色。

You are asking for two different things, do you want the drawables or the colorcode? 您要两种不同的东西,您想要绘制对象还是颜色代码?

Anyway, you can find the name of the drawables here: http://androiddrawableexplorer.appspot.com/ I don't know if you can use them directly from your app or if you have to save them to your drawables folder first, but you can find them in your sdk. 无论如何,您可以在此处找到可绘制对象的名称: http : //androiddrawableexplorer.appspot.com/我不知道您是否可以直接从应用程序中使用它们,还是必须先将它们保存到drawables文件夹中,您可以在SDK中找到它们。 If you want the colorcodes, use gimp to extract them from the pictures. 如果需要颜色代码,请使用gimp从图片中提取颜色代码。

Selectors are what you're looking for. 选择器是您想要的。 Google around for tutorials. 谷歌周围的教程。 Here's one . 这是一个

I Suppose you can find the Default Selector in the Android Source Code. 我想您可以在Android源代码中找到默认选择器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM