如何设置样式按钮

时间:2016-06-27 15:54:35

标签: android xml button

我正在寻找一种方式来设置我的按钮样式,就像一个旧问题(How to create standard Borderless buttons (like in the design guidline mentioned)?)中的例子一样,但有些事情从答案中遗漏了。

如何向XML元素添加多个值,尤其是android:background ="";

我想出了如何使我的按钮无边框,但我希望它们有一个非常薄的边框和不同的背景颜色。我在网上看到很多提示,但我找不到将所有项目放在代码中的方法。下面是一个复制粘贴的图像,顶部代表我想用按钮名称实现的布局和按钮右侧的缩略图图像,按钮的颜色与应用程序的背景颜色不同,下面是是我试图实现的边框样式的复制粘贴图像,薄,触摸按钮之间的边框。我到处寻找并尝试了许多想法,但似乎没有一个正常工作,或者有些要求我有

android:background="?android:attr/selectableItemBackground"

但这与

相互作用

android:background="#22272D"

enter image description here

我需要将文本保留在按钮中,因为我的应用程序会将按钮文本转换为用户手机的语言,因此我无法将整个按钮只显示为图像。下面是我的XML和输出,如何改变它的任何建议都会有很大的帮助!

<LinearLayout
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:weightSum="6"
tools:context="com.aid.travelers.myapplication.Transportation">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/transportation_page"
        android:textSize="40sp"
        android:textColor="#000000"/>

    <Button
        android:layout_width="match_parent"
        android:text="@string/airport"
        android:id="@+id/AirportButton"
        android:layout_height="60dp"
        android:layout_weight="1"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:background="?android:attr/selectableItemBackground"/>

  <Button
        android:layout_width="match_parent"
        android:text="@string/bicycle"
        android:id="@+id/BicycleButton"
        android:layout_height="60dp"
        android:layout_weight="1"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:background="?android:attr/selectableItemBackground"/>

enter image description here

1 个答案:

答案 0 :(得分:0)

我不确定我是否收到您的问题,但我想您想要自定义按钮的背景,并在按下时产生触控效果。

看看这个答案: https://stackoverflow.com/a/7176006/5446285

您应该在drawable文件夹中创建自己的资源文件background.xml(例如)。 代码应该是:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
        android:drawable="@drawable/background_selected"/> <!-- pressed -->
    <item android:drawable="@drawable/background_unselected" /> <!-- default -->
</selector>

您现在应该在drawable文件夹中创建另外两个文件:background_selected.xml和background_unselected.xml。 为此,如果你想要一个薄边框,我建议你这样:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
    <shape android:shape="rectangle">
        <solid android:color="#7c7c7c" />
    </shape>
</item>
<item android:bottom="2dp" android:top="2dp" android:right="2dp" android:left="2dp">
    <shape android:shape="rectangle" >
        <solid android:color="#a0a0a0" />
    </shape>
</item>

然后在xml中设置按钮的背景:

android:background="@drawable/background"