Android API 4.1中,如何在按钮点击时更改背景颜色?

4

I want to know that on button click how the background color changed here and it takes place more than the button size..Can anybody tell e that from where the background color is coming to this?? inside the android api 4.1, at path: Android API-res-layout-log_text_box_1.xml file looks like this.

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/add"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/log_text_box_1_add_text"/>

    <com.example.android.apis.text.LogTextBox
        android:id="@+id/text"
        android:background="@drawable/box"
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:scrollbars="vertical"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/log_text_box_1_do_nothing_text"/>

</LinearLayout>


抱歉,我无法理解您的意图。请问您从哪里复制了这个布局? - K_Anas
你是否在寻找 @drawable/box? - K_Anas
Android API-res-layout-log_text_box_1.xml。 - Narendra Pal
Android API-res-layout-log_text_box_1.xml。我从这里复制了它。为了更方便,运行Android SDK中的Android API演示,然后转到Text-LogTextBox。之后,新的活动将打开,其中有两个按钮,第一个是ADD,另一个是Do Nothing。当您单击任何按钮时,会看到该按钮的背景颜色正在更改,但更改颜色占用的空间比按钮大。为什么?如何解决?这正是我的问题。如果您知道,请回复。先感谢您... - Narendra Pal
1个回答

5

这种行为是Holo主题下默认的Android样式按钮,使用这些按钮的选择器位于 您的sdk路径\platforms\android-16\data\res\drawable,文件名为:btn_default_holo_dark.xmlbtn_default_holo_light.xml

第一个选择器所使用的可绘制对象为:

btn_default_normal_holo_dark.9

enter image description here

btn_default_disabled_holo_dark.9

enter image description here

btn_default_disabled_focused_holo_dark.9

enter image description here

btn_default_focused_holo_dark.9

enter image description here

btn_default_pressed_holo_dark.9

在此输入图片描述

最后一个9-patch图像是导致按钮超出边框效果的原因,希望这就是你要找的。


谢谢你的回答。我会去查看的。非常感谢。 - Narendra Pal
@user1395259,drawable文件位于Android SDK的路径\platforms\android-16\data\res\drawable-mdpi中,不客气! - K_Anas

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接