Jak utworzyć standardowe przyciski bez obramowania(jak w wytycznych dotyczących projektowania)?

Właśnie sprawdzałem wytyczne projektowe i zastanawiałem się nad przyciskami bez obramowania. Próbowałem znaleźć źródło, ale sam nie mogę tego połączyć. Czy jest to zwykły widżet przycisku, ale dodajesz Niestandardowy (domyślny Android) Styl? Jak zrobić te przyciski bez obramowania (oczywiście można ustawić tło na puste, ale wtedy nie mam dzielnika)?

Tutaj linki do projektu wytyczne:

Tutaj wpisz opis obrazka

Author: Cœur, 2012-01-13

19 answers

Aby wyjaśnić pewne zamieszanie:

Odbywa się to w 2 krokach: ustawienie atrybutu tła przycisku na android: attr / selectableItemBackground tworzy przycisk z informacją zwrotną, ale bez tła.

android:background="?android:attr/selectableItemBackground"

Linia dzieląca przycisk bezramkowy od reszty układu jest wykonywana przez Widok Z tłem android: attr / dividerVertical

android:background="?android:attr/dividerVertical"

Dla lepszego zrozumienia poniżej znajduje się układ dla kombinacji przycisków bezramkowych OK / Cancel na u dołu ekranu (jak na prawym zdjęciu powyżej).

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:layout_alignParentBottom="true">
        <View
            android:layout_width="match_parent"
            android:layout_height="1dip"
            android:layout_marginLeft="4dip"
            android:layout_marginRight="4dip"
            android:background="?android:attr/dividerVertical"
            android:layout_alignParentTop="true"/>
        <View
            android:id="@+id/ViewColorPickerHelper"
            android:layout_width="1dip"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_alignParentBottom="true"
            android:layout_marginBottom="4dip"
            android:layout_marginTop="4dip"
            android:background="?android:attr/dividerVertical" 
            android:layout_centerHorizontal="true"/>
        <Button
            android:id="@+id/BtnColorPickerCancel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_toLeftOf="@id/ViewColorPickerHelper"
            android:background="?android:attr/selectableItemBackground"
            android:text="@android:string/cancel" 
            android:layout_alignParentBottom="true"/>
        <Button
            android:id="@+id/BtnColorPickerOk"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:background="?android:attr/selectableItemBackground"
            android:text="@android:string/ok" 
            android:layout_alignParentBottom="true" 
            android:layout_toRightOf="@id/ViewColorPickerHelper"/>
    </RelativeLayout>
 161
Author: KarlKarlsom,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-07-01 01:54:09

Po prostu dodaj następujący atrybut stylu w znaczniku Button:

    style="?android:attr/borderlessButtonStyle"

Źródło: http://developer.android.com/guide/topics/ui/controls/button.html#Borderless

Następnie można dodać dzielniki jak w odpowiedź Karla .

 46
Author: Diego V,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 11:55:19

Późna odpowiedź, ale wiele poglądów. Ponieważ APIs

Niech twój pojemnik będzie miał pożądany kolor (może być przezroczysty). Następnie daj przyciskom selektor z domyślnym przezroczystym kolorem i trochę koloru po naciśnięciu. W ten sposób będziesz miał przezroczysty przycisk, ale zmieni kolor po naciśnięciu (jak holo). Możesz również dodać animację (jak holo). Selektor powinien być coś takiego:

res/drawable/selector_transparent_button.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" 
          android:exitFadeDuration="@android:integer/config_shortAnimTime">
     <item android:state_pressed="true"
         android:drawable="@color/blue" />

   <item android:drawable="@color/transparent" />
</selector>

I przycisk powinien have android:background="@drawable/selector_transparent_button"

PS: niech kontener będzie miał dzielniki (android:divider='@android:drawable/... dla API

PS [Newbies]: powinieneś zdefiniować te kolory w values / colors.xml

 23
Author: aacotroneo,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-08-16 08:51:47

Dla tych, którzy chcą przycisków bez obramowania, ale nadal animowanych po kliknięciu. Dodaj to w przycisku.

style="?android:attr/borderlessButtonStyle"

Jeśli chcesz dzielnik / linię między nimi. Dodaj to w układzie liniowym.

style="?android:buttonBarStyle"

Podsumowanie

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:orientation="horizontal"
   style="?android:buttonBarStyle">

    <Button
        android:id="@+id/add"
        android:layout_weight="1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/add_dialog" 
        style="?android:attr/borderlessButtonStyle"
        />

    <Button
        android:id="@+id/cancel"
        android:layout_weight="1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/cancel_dialog" 
        style="?android:attr/borderlessButtonStyle"
        />

</LinearLayout>
 17
Author: aLIEz,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-09-01 17:21:02

Dla stylu materiału dodaj style="@style/Widget.AppCompat.Button.Borderless" podczas korzystania z biblioteki AppCompat.

 7
Author: Roel,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-10-05 17:46:07

Z iosched app source wymyśliłem tę ButtonBar klasę:

/**
 * An extremely simple {@link LinearLayout} descendant that simply reverses the 
 * order of its child views on Android 4.0+. The reason for this is that on 
 * Android 4.0+, negative buttons should be shown to the left of positive buttons.
 */
public class ButtonBar extends LinearLayout {

    public ButtonBar(Context context) {
        super(context);
    }

    public ButtonBar(Context context, AttributeSet attributes) {
        super(context, attributes);
    }

    public ButtonBar(Context context, AttributeSet attributes, int def_style) {
        super(context, attributes, def_style);
    }

    @Override
    public View getChildAt(int index) {
        if (_has_ics)
            // Flip the buttons so that "OK | Cancel" becomes "Cancel | OK" on ICS
            return super.getChildAt(getChildCount() - 1 - index);

        return super.getChildAt(index);
    }

    private final static boolean _has_ics = Build.VERSION.SDK_INT >= 
                                        Build.VERSION_CODES.ICE_CREAM_SANDWICH;
}

To będzie LinearLayout, w które wejdą przyciski " OK " i "anuluj" i poradzą sobie z umieszczeniem ich w odpowiedniej kolejności. Następnie umieść to w układzie, w którym chcesz umieścić przyciski:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:divider="?android:attr/dividerHorizontal"
          android:orientation="vertical"
          android:showDividers="middle">
    <!--- A view, this approach only works with a single view here -->
    <your.package.ButtonBar style="?android:attr/buttonBarStyle"
        android:id="@+id/buttons"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:weightSum="1.0">
        <Button style="?android:attr/buttonBarButtonStyle"
            android:id="@+id/ok_button"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.5"
            android:text="@string/ok_button" />
        <Button style="?android:attr/buttonBarButtonStyle"
            android:id="@+id/cancel_button"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.5"
            android:text="@string/cancel_button" />
    </your.package.ButtonBar>
</LinearLayout>

To daje wygląd okna dialogowego z przyciskami bez obramowania. Te atrybuty można znaleźć w res w ramach. buttonBarStyle wykonuje pionowy dzielnik i wyściółkę. {[5] } jest ustawione jako borderlessButtonStyle dla tematu Holo, ale uważam, że powinien to być najbardziej solidny sposób wyświetlania go, ponieważ framework chce go wyświetlić.

 5
Author: Dandre Allison,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-09-19 19:21:54

Spójrz na atrybuty motywubuttonBarStyle, buttonBarButtonStyle, i borderlessButtonStyle.

 4
Author: Josh Lee,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-01-26 17:39:23

Przyciski można również tworzyć bezramkowo za pomocą kodu:

TypedValue value= new TypedValue();
getApplicationContext().getTheme().resolveAttribute(android.R.attr.selectableItemBackground, value, true);
 myButton.setBackgroundResource(value.resourceId);
 4
Author: Isj,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-08-31 09:44:17

Dla tych, którzy chcą programowo utworzyć przycisk bezramkowy dla API > = 8

ImageButton smsImgBtn = new ImageButton(this);
//Sets a drawable as the content of this button
smsImgBtn.setImageResource(R.drawable.message_icon);    
//Set to 0 to remove the background or for bordeless button
smsImgBtn.setBackgroundResource(0);
 2
Author: Sohail,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-04-21 21:17:55

Innym rozwiązaniem, które powinno działać zarówno na starszej, jak i nowszej platformie android, jest użycie

android:background="@android:color/transparent"

Atrybut widoku przycisków. Ale po dodaniu powyżej przycisk linii nie dostarczy informacji zwrotnej dotyku.

Aby zapewnić sprzężenie zwrotne z dotykiem, Dodaj następujący kod do klasy aktywności

button.setOnTouchListener(new View.OnTouchListener() {          
    @Override
    public boolean onTouch(View view, MotionEvent event) {
        switch (event.getAction())
        {
            case MotionEvent.ACTION_DOWN:    
                ((Button)view).setBackgroundColor(Color.LTGRAY);
                break;
            case MotionEvent.ACTION_UP:
                ((Button)view).setBackgroundColor(Color.TRANSPARENT);
        }
        return false;
    }
});
Dla mnie działa dobrze.
 2
Author: prodev,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-06-21 13:28:31

Dla wszystkich, którzy wciąż szukają:

Dziedzicz swój własny styl dla Holo buttonbars:

<style name="yourStyle" parent="@android:style/Holo.ButtonBar">
  ...
</style>

Lub Światło Holo:

<style name="yourStyle" parent="@android:style/Holo.Light.ButtonBar">
  ...
</style>

I dla przycisków Holo bez obramowania:

<style name="yourStyle" parent="@android:style/Widget.Holo.Button.Borderless.Small">
  ...
</style>

Lub Światło Holo:

<style name="yourStyle" parent="@android:style/Widget.Holo.Light.Button.Borderless.Small">
  ...
</style>
 2
Author: silversmurf,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-06-30 15:43:52

Użyj poniższego kodu w pliku xml. Użyj android: background= "#00000000", aby mieć przezroczysty kolor.

<Button
   android:id="@+id/btnLocation"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:background="#00000000"
   android:text="@string/menu_location"
   android:paddingRight="7dp"
/>
 1
Author: Kayuri Ravrani,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-05-27 03:38:05

Możesz użyć AppCompat Support Library dla przycisku bez obramowania.

Możesz zrobić przycisk bez obramowania w ten sposób:

<Button
    style="@style/Widget.AppCompat.Button.Borderless"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="16dp" 
    android:text="@string/borderless_button"/>

Możesz zrobić kolorowy przycisk bez obramowania w następujący sposób:

<Button
    style="@style/Widget.AppCompat.Button.Borderless.Colored"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="16dp" 
    android:text="@string/borderless_colored_button"/>
 1
Author: Rajesh,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-12-06 13:51:09

W ten sposób programowo tworzysz przycisk bez obramowania (płaski) bez użycia XML

ContextThemeWrapper myContext = new ContextThemeWrapper(this.getActivity(), 
   R.style.Widget_AppCompat_Button_Borderless_Colored);

Button myButton = new Button(myContext, null, 
   R.style.Widget_AppCompat_Button_Borderless_Colored);
 1
Author: Manuel,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-09-27 07:55:20

Z jakiegoś powodu ani style="Widget.Holo.Button.Borderless" Ani android:background="?android:attr/selectableItemBackground" nie zadziałały dla mnie. Aby być bardziej precyzyjnym Widget.Holo.Button.Borderless zrobił to na Androidzie 4.0, ale nie działał na Androidzie 2.3.3. Co mi się udało w obu wersjach To android:background="@drawable/transparent" i ten XML w res/drawable / transparent.xml:

<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle" >
</shape>
/ Align = "center" bgcolor = "# e0ffe0 " / cesarz chin / / align = center /
 0
Author: Emperor Orionii,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-04-19 14:38:54

Świetny pokaz slajdów na Jak osiągnąć pożądany efekt z Googles Nick Butcher (Zacznij od slajdu 20). Używa standardowego Androida @attr do stylizacji przycisku i dzielnika.

 0
Author: Moritz,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-06-08 09:26:08

Dodając do górnej odpowiedzi Możesz również użyć widoków z ciemnoszarym kolorem tła w układzie liniowym.

<View
    android:layout_width="match_parent"
    android:layout_height="1dip"
    android:layout_marginBottom="4dip"
    android:layout_marginLeft="4dip"
    android:layout_marginRight="4dip"
    android:layout_marginTop="4dip"
    android:background="@android:color/darker_gray"/>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="4dip"
    android:orientation="horizontal"
    android:weightSum="1">

    <Button
        android:id="@+id/button_decline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_weight="0.50"
        android:background="?android:attr/selectableItemBackground"
        android:padding="10dip"
        android:text="@string/decline"/>

    <View
        android:layout_width="1dip"
        android:layout_height="match_parent"
        android:layout_marginLeft="4dip"
        android:layout_marginRight="4dip"
        android:background="@android:color/darker_gray"/>

    <Button
        android:id="@+id/button_accept"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="10dp"
        android:layout_weight="0.50"
        android:background="?android:attr/selectableItemBackground"
        android:padding="10dip"
        android:text="@string/accept"/>
</LinearLayout>

Jeśli linia jest pozioma, należy ustawić wysokość na 1dip i szerokość, aby pasowały do linii nadrzędnej i odwrotnie, jeśli linia jest pionowa.

 0
Author: Munaz,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-03-11 16:20:49

Jeśli chcesz osiągnąć to samo programowo:

(jest to C# , ale łatwo przełączyć na Javę)

Button button = new Button(new ContextThemeWrapper(Context, Resource.Style.Widget_AppCompat_Button_Borderless_Colored), null, Resource.Style.Widget_AppCompat_Button_Borderless_Colored);

Mecz

    <Button
       style="@style/Widget.AppCompat.Button.Borderless.Colored"
    .../>
 0
Author: Yohan Dahmani,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 14:22:58

Spróbuj tego kodu, aby programowo usunąć tło drawable (@drawable/bg), po prostu musimy podać null jako parametr.

Button btn= new Button(this);
btn.setText("HI");
btn.setBackground(null);
 0
Author: Ramesh kumar,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-09-27 09:43:50