简体   繁体   English

造型底部按钮android

[英]Styling bottom buttons android

I have two buttons in bottom of screen, but how can I style stock buttons to style of my concept: 我在屏幕底部有两个按钮,但是如何根据我的概念来设置股票按钮的样式:

我的概念

Here my code layout file: 这是我的代码布局文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal">
    <Button
        android:id="@+id/button1"
        android:layout_height="wrap_content"
        android:layout_width="180sp"
        android:text="Далее"
        android:layout_weight="1"
        android:layout_gravity="bottom"
        android:layout_alignTop="@+id/button2"
        android:layout_alignParentRight="true">
    </Button>
    <Button
        android:id="@+id/button2"
        android:layout_width="180dp"
        android:layout_height="wrap_content"
        android:text="Пропустить"
        android:layout_weight="1"
        android:layout_gravity="bottom"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true">
    </Button>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Добро пожаловать!"
        android:id="@+id/textView"
        android:layout_marginTop="29dp"
        android:fontFamily="sans-serif-thin"
        android:phoneNumber="false"
        android:textSize="33sp"
        android:textColor="@color/text_color"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="25dp" />
</RelativeLayout>

There is multiple ways to do this when building an android application. 构建Android应用程序时,有多种方法可以做到这一点。

The first and simplest is to set your background to an image. 第一个也是最简单的方法是将背景设置为图像。 To do this add a image to your drawable folders in the res folder, add the correct size images to each drawable folder to correct display across multiple platforms. 为此,将图像添加到res文件夹中的可绘制文件夹中,将正确大小的图像添加到每个可绘制文件夹中,以在多个平台上正确显示。 Once you have imported your image simply put the following line in the layout xml for your button 导入图像后,只需将以下行放入按钮的布局xml中

android:background = "@drawable/imported_image"

The second is to create an selector android XML file. 第二个是创建一个选择器android XML文件。 This will let you set the background image for different states of the buttons. 这将使您为按钮的不同状态设置背景图像。 I usually create a file under my res folder in my project called drawable and put the xml file there. 我通常在项目的res文件夹下创建一个文件,称为drawable,并将xml文件放在该文件中。 The selector file would look something like this 选择器文件看起来像这样

<?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/large_button_on" />  

<item android:drawable="@drawable/large_button_off" />

</selector>

the code to put in your Layout xml inside your button should be 放在按钮内的Layout xml中的代码应为

android:background="@drawable/custom_button"

The final way without using any images is to define a shape android xml file for the button. 不使用任何图像的最终方法是为按钮定义一个形状android xml文件。 This will instruct how you want the button to be drawn on screen. 这将指示您如何在屏幕上绘制按钮。 This xml should be placed in your project the same way we did with the other. 将此xml放置在您的项目中的方式应与其他方式相同。

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

  <gradient android:startColor="#000000" 
    android:endColor="#0000ff"
    android:angle="270" />

  <stroke android:width="1dp" android:color="#ffffff" />

</shape>

again if you use this method put the following in your layout xml for your button 再次,如果您使用此方法,则将以下内容放入按钮的布局xml中

android:background="@drawable/custom_button"

Hope these methods of styling help. 希望这些样式方法有所帮助。

You can create an drawable xml file like this and have a different drawable for the different states of you button 您可以像这样创建一个可绘制的xml文件,并针对按钮的不同状态创建一个不同的可绘制对象

res/drawable/my_button_drawable.xml res / drawable / my_button_drawable.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/button_pressed"
          android:state_pressed="true" />
    <item android:drawable="@drawable/button_focused"
          android:state_focused="true" />
    <item android:drawable="@drawable/button_default" />
</selector>

Then in your button's declaration add the line: 然后在按钮的声明中添加以下行:

android:background="@drawable/my_button_drawable"  />

Android Developer Buttons Android开发人员按钮

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM