Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android将视图样式设置为类似彩色按钮_Android_Xml_Android Layout - Fatal编程技术网

Android将视图样式设置为类似彩色按钮

Android将视图样式设置为类似彩色按钮,android,xml,android-layout,Android,Xml,Android Layout,所以我想设计一个线性布局的样式,使其看起来像一个按钮。我使用的是LinearLayout,因为我使用的图标是“Font-awesome”,我需要将图标放在按钮内,所以我有一个LinearLayout,它有两个TextView作为子项。一个用于图标,另一个用于文本 我想要的是使线性布局看起来像默认样式按钮,但颜色不同。对棒棒糖和更新的它也应该有连锁反应 我通过添加一个背景色来做到这一点,但它只适用于棉花糖,在KitKat上,按钮有默认颜色。这是我的密码: 布局: <LinearLayout

所以我想设计一个
线性布局
的样式,使其看起来像一个按钮。我使用的是
LinearLayout
,因为我使用的图标是“Font-awesome”,我需要将图标放在按钮内,所以我有一个
LinearLayout
,它有两个
TextView
作为子项。一个用于图标,另一个用于文本

我想要的是使
线性布局
看起来像默认样式按钮,但颜色不同。对棒棒糖和更新的它也应该有连锁反应

我通过添加一个
背景色来做到这一点,但它只适用于棉花糖,在KitKat上,按钮有默认颜色。这是我的密码:

布局:

<LinearLayout
        android:id="@+id/btn_register"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clickable="true"
        style="@style/MyTheme.Button" >
        <TextView
            android:id="@+id/ic_register"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@android:color/white"
            android:text="@string/fa_user_plus"
            android:textSize="16dp"
            android:paddingRight="4dp" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@android:color/white"
            android:text="Register as a new user"
            android:textSize="16dp" />
  </LinearLayout>

风格:

<style name="MyTheme.Button" parent="@android:style/Widget.DeviceDefault.Button">
    <item name="android:backgroundTint">@color/accent</item>
</style>

@颜色/口音

如果这对您有用:

    <Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="103dp"
    android:layout_marginTop="46dp"
    android:textColor="@android:color/white"
    android:background="@drawable/your_icon"
    android:gravity="right|center_horizontal"
    android:text="Register as a new user" />


您可以根据需要自定义或更改它,用重力改变文本的位置。

解决方案:我创建了两个不同的背景选择器—一个用于API 21,另一个用于旧设备

可绘制/btn_red.xml:

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

    <item android:state_pressed="true">
        <shape android:shape="rectangle">
            <solid android:color="#88FFFFFF" />
            <corners android:radius="2dp"/>
        </shape>
    </item>         

    <item>
        <shape android:shape="rectangle">
            <solid android:color="@color/accent" />
            <corners android:radius="2dp"/>
        </shape>
    </item>
</selector>
<ripple
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="#88FFFFFF"> <!-- The ripple color -->

    <!-- the normal bg color will be light white -->
    <item>
        <shape android:shape="rectangle">
            <solid android:color="@color/accent" />
            <corners android:radius="2dp"/>
        </shape>
    </item>
</ripple>

drawable-v21/btn_red.xml:

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

    <item android:state_pressed="true">
        <shape android:shape="rectangle">
            <solid android:color="#88FFFFFF" />
            <corners android:radius="2dp"/>
        </shape>
    </item>         

    <item>
        <shape android:shape="rectangle">
            <solid android:color="@color/accent" />
            <corners android:radius="2dp"/>
        </shape>
    </item>
</selector>
<ripple
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="#88FFFFFF"> <!-- The ripple color -->

    <!-- the normal bg color will be light white -->
    <item>
        <shape android:shape="rectangle">
            <solid android:color="@color/accent" />
            <corners android:radius="2dp"/>
        </shape>
    </item>
</ripple>


尝试使用android:background而不是android:backgroundTint@Joakim这将删除所有按钮样式-波纹和圆角。也许你可以使用一个简单的按钮,以图标为背景,将文本放置在你想要的位置,并与布局对齐。这比布局简单,但我不知道是否对您有用。不能使用图标作为背景,因为它不是可绘制的。我正在使用“字体真棒”图标字体来显示图标。啊,我明白了。如果你用文字,一些简单的东西,用绘画,photoshop。。。那些图标呢?你可以尽可能地制作按钮,并且有一个带有你的首选项、文本、图标的真实按钮……然后我必须为不同的屏幕密度制作多个版本的可绘制按钮,我必须为每个按钮都这样做。