【问题标题】:How to make a button of this shape in android?如何在android中制作这种形状的按钮?
【发布时间】:2016-06-15 09:19:22
【问题描述】:

如何为按钮制作此背景 xml。

【问题讨论】:

  • 一般在xml中是做不到的,需要写一个自定义的Drawable

标签: android android-layout android-studio android-button


【解决方案1】:

使用 xml fileinside Drawable 文件夹并将其分配给 TextView 作为 android:background="@drawable/xmlFile"

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <corners
        android:radius="100dip"/>
    <solid
        android:color="#FFF" />
    <stroke
        android:width="2dip"
        android:color="#F00" />
    <padding
        android:left="6dip"
        android:right="6dip"
        android:top="5dip"
        android:bottom="5dip" />
</shape>

【讨论】:

    【解决方案2】:

    像这样将按钮背景创建为可绘制资源。

    button_background.xml

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    
        <item android:state_focused="true">
            <shape>
                <solid android:color="#f5f5f5" />
    
                <corners android:radius="8dp" />
                <stroke android:width="4dp" android:color="#FF0000" />
                <padding android:bottom="16dp" android:left="16dp" android:right="16dp" android:top="16dp" />
            </shape>
        </item>
        <item android:state_enabled="true" android:state_focused="false">
            <shape>
    
                <solid android:color="#ffffff" />
                <corners android:radius="8dp" />
                <stroke android:width="4dp" android:color="#FF0000" />
                <padding android:bottom="16dp" android:left="16dp" android:right="16dp" android:top="16dp" />
            </shape>
        </item>
    
    
    </selector>
    

    然后只需在您的按钮上使用它

    <Button 
       android:layout_width="match_parent"
       android:layout_height="wrap_content" 
       android:text="Your Text"
       android:textColor="#FF0000"
       android:background="@drawable/button_background" />
    

    【讨论】:

      【解决方案3】:

      试试这个

      http://tips.androidhive.info/2013/09/android-layout-rounded-corner-border/

      http://android--code.blogspot.in/2015/01/android-rounded-corners-button.html

      在drawable文件夹中添加一个xml文件并添加这段代码

      <?xml version="1.0" encoding="utf-8"?>
      <shape
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:shape="rectangle">
      
      <!-- view background color -->
      <solid
          android:color="#a9c5ac" >
      </solid>
      
      <!-- view border color and width -->
      <stroke
          android:width="3dp"
          android:color="#1c1b20" >
      </stroke>
      
      <!-- If you want to add some padding -->
      <padding
          android:left="4dp"
          android:top="4dp"
          android:right="4dp"
          android:bottom="4dp"    >
      </padding>
      
      <!-- Here is the corner radius -->
      <corners
          android:radius="10dp"   >
      </corners>
      
      </shape>
      

      并且对于按钮将背景更改为此 xml 为 android:background="@drawable/yourxml"

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多