【问题标题】:Design XML Button Android设计 XML 按钮 Android
【发布时间】:2017-10-01 21:49:18
【问题描述】:

这是一个使用 XML 设计按钮的应用示例。

我怎样才能拥有相同的design with XML

如何使我的按钮look like it is floating 如下图所示?

【问题讨论】:

    标签: android xml android-layout user-interface


    【解决方案1】:

    我认为您需要使用带有分层列表的可绘制形状,这是一个顶部和底部具有不同颜色的按钮示例(投影效果)。您将其设置为 Button 的背景属性。

    <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    
        <item>
            <shape android:shape="rectangle" >
                <corners android:radius="2dp" />
    
                <solid android:color="@color/button_border_dark" />
            </shape>
        </item>
        <item android:top="1dp">
            <shape android:shape="rectangle" >
                <corners android:radius="2dp" />
    
                <solid android:color="@color/button_border_light" />
            </shape>
        </item>
        <item
            android:bottom="1dp"
            android:top="1dp">
            <shape android:shape="rectangle" >
                <corners android:radius="2dp" />
    
                <solid android:color="@color/button_general_color" />
            </shape>
        </item>
    
    </layer-list>
    

    【讨论】:

    • 我用我想要的颜色修改你的代码。如何使阴影部分更厚?
    • 更改这些属性:android:bottom="1dp", android:top="1dp"(改为 2dp 或其他)
    • 最后一件事非常感谢。我怎样才能让我的按钮看起来像是被点击了?
    • 也许搜索 SO?那是一个不同的问题(搜索可绘制选择器)。
    【解决方案2】:

    这是按钮的 XML。您还可以使用自定义字体和阴影使其成为您想要的方式。

     <Button 
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:background="@android:color/holo_blue_dark"
                android:textColor="@android:color/white"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                android:layout_gravity="center"
                android:shadowColor="@android:color/holo_blue_light"
                android:id="@+id/btnClickMe"
                android:text="Click Me!"
                />
    

    【讨论】:

    【解决方案3】:

    用这个....

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:gravity="center"
      android:orientation="vertical" >
    
      <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginLeft="25dp"
        android:layout_marginRight="25dp"
        android:background="@drawable/selected" // selected is the name of your custom file
        android:text="Register"
        android:textColor="#fff" />
    
     <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginLeft="25dp"
        android:layout_marginRight="25dp"
        android:background="#37a8f7"
        android:text="Login"
        android:layout_marginTop="15dp"
        android:textColor="#fff" />
    
    </LinearLayout>
    

    您可以在 drawable 文件夹中为红色按钮制作自定义文件 selected.xml

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android">
      <solid android:color="#ff0000"/>
      <corners android:radius="1dp"/>
      <padding android:left="3dp" android:top="2dp"
            android:right="3dp" android:bottom="2dp" />
    </shape>
    

    并将其设置为您的红色按钮。

    你可以和你的蓝色按钮一样。

    【讨论】:

    • 阴影效果怎么样?谢谢顺便说一句。
    • 也可以动态给出 button.setShadowLayer(4,0,0,Color.BLACK);
    猜你喜欢
    • 2015-03-08
    • 1970-01-01
    • 1970-01-01
    • 2014-08-20
    • 1970-01-01
    • 1970-01-01
    • 2015-07-06
    • 2016-06-28
    • 2015-01-04
    相关资源
    最近更新 更多