2014-01-11 50 views
-1

以下是使用XML设计按钮的应用程序示例。设计XML按钮Android

我怎么能有相同的design with XML

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

Sample

回答

4

我想你会需要使用一个形状绘制具有分层列表,这里要说的是对顶部和底部(阴影效果)不同颜色的按钮的例子。您将此设置为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> 
+0

我用我想要的颜色修改你的代码。我怎样才能让阴影部分更厚? – Toshi

+1

更改这些属性:android:bottom =“1dp”,android:top =“1dp”(至2dp或其他) – Booger

+0

谢谢你最后一件事。我怎么让我的按钮看起来像点击? – Toshi

1

用这个....

<?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> 

你可以在红色按钮绘制文件夹自定义文件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> 

并将其设置为红色按钮。

而且你可以使你的蓝色按钮相同。

+0

阴影效应如何?谢谢btw。 – Toshi

+0

你也可以动态给button.setShadowLayer(4,0,0,Color.BLACK); – Piyush

1

这里是按钮的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!" 
      /> 
+0

你是什么意思的字体? – Toshi

+1

它意味着字体。如果你想改变字体请参考这个http://stackoverflow.com/questions/5497258/android-help-with-changing-button-font-type-how –

+0

谢谢!我可以问,如果阴影也是用xml实现的吗? – Toshi