自定义按钮外观

时间:2015-10-21 15:48:44

标签: android android-layout android-studio

我在我的活动中设置了一个按钮。目前的代码:

switch (choice) {
case 1:    //finish add passenger
    System.out.println(" 1 - Add passenger");
    System.out.println(mySubMenu.displayMenu());
    int subChoice = mySubMenu.getChoice();
    switch (subChoice) {
        case 1:
            try {
                System.out.println("Please enter the passengers forename");
                String aforename = strKeyB.next();
                System.out.println("Please enter the passengers surname");
                String surname = strKeyB.next();

                System.out.println("Please enter the passengers nationality");
                String nationality = strKeyB.next();

                System.out.println("Please enter the passenger age");
                int age = intKeyB.nextInt();
                System.out.println(pass1.pasengerAdd(aforename, surname, nationality, age));
            } catch (Exception ex) {
                System.out.println(ex.toString());

            }
            break;  // here you miss case 1: in switch(subchoice) when exception

        case 2:
            try {
                System.out.println("Please enter the passengers forename");
                String onforename = strKeyB.next();
                System.out.println("Please enter the passengers surname");
                String onSurname = strKeyB.next();
                pass1.pasengerAdd(onforename, onSurname);

            } catch (Exception ex) {
                System.out.println(ex.toString());

            }
            break;

//
// more code until...
//

break; 

case 2:  // here you miss case 1: in switch(choice) 
    try {
        System.out.println(" 2 - Delete Passenger");

它目前有白色背景。我希望背景是透明的,以便用户只能看到文本。 当用户点击按钮时,我只想在没有背景的按钮周围出现绿色边框。

我想知道这是否只能用xml完成​​?

3 个答案:

答案 0 :(得分:0)

您需要在选择项目时制作一个drawable,然后将其作为背景应用。所以简而言之,是的,只有xml可以这样做,但你需要另外制作一个文件。

与此类似的东西

<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_pressed="true"
   android:drawable="@drawable/button_selected" /> <!-- pressed -->
  <item android:state_focused="true"
   android:drawable="@drawable/button_focus" /> <!-- focused -->
  <item android:drawable="@drawable/button_nothing" /> <!-- default -->
</selector>

button_selected.xml

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

    <solid android:color="#00FF00"/>
</shape>

答案 1 :(得分:0)

是的,你可以。您需要做的就是将可绘制的状态列表指定为按钮的背景。状态列表drawable inturn包含对Shape Drawable的引用。

在此处阅读所有相关内容(Shape Drawable和State List主题):http://developer.android.com/guide/topics/resources/drawable-resource.html

答案 2 :(得分:0)

在可绘制文件夹中创建一个xml文件并粘贴此

 <?xml version="1.0" encoding="utf-8"?>
 <selector xmlns:android="http://schemas.android.com/apk/res/android" >
 <item android:state_pressed="true" >
     <shape android:shape="rectangle"  >
         <corners android:radius="10dip" />
         <stroke android:width="1dip" android:color="#fff" />
         <gradient  android:angle="-90"  android:startColor="@color/primary" android:endColor="@color/primary_dark"  />
     </shape>
 </item>
<item android:state_focused="true">
     <shape android:shape="rectangle"  >
         <corners android:radius="10dip" />
         <stroke android:width="1dip" android:color="#fff" />
         <solid  android:color="#FF7300"/>       
     </shape>
 </item>    
 <item >
    <shape android:shape="rectangle"  >
         <corners android:radius="10dip" />
         <stroke android:width="1dip" android:color="#fff" />
         <gradient  android:angle="-90"  android:startColor="@color/primary" android:endColor="@color/primary" />
     </shape>
 </item>
</selector> 

现在设置按钮的背景,如

android:background="@drawable/yourxml"