簡體   English   中英

為什么在android中設置背景時按鈕會變大

[英]Why button gets bigger when the background is set in android

當我使用 xml 中的android:background屬性為其設置背景時,我遇到了Button視圖的問題。 在設置背景之前,按鈕具有默認大小。 但是當我應用一種顏色作為背景時,它變得越來越大,即使我沒有設置不同的widthheight

這是我將背景屬性設置為按鈕之前的 xml 布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
        android:background="@color/white"
        android:layout_gravity="center_horizontal"
        android:id="@+id/btn_row_option_done"
        android:text="Done"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Button
        android:background="@color/white"
        android:layout_gravity="center_horizontal"
        android:text="Edit"
        android:id="@+id/btn_row_option_edit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Button
        android:layout_gravity="center_horizontal"
        android:text="Delete"
        android:id="@+id/btn_row_option_delete"
        android:background="@color/red"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Button
        android:layout_gravity="center_horizontal"
        android:text="Cancel"
        android:id="@+id/btn_row_option_cancel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

所以取消按鈕是默認大小,如截圖所示。

在此處輸入圖片說明

但是當我像這樣在取消按鈕上設置顏色時

 <Button
        android:background="@color/white"
        android:layout_gravity="center_horizontal"
        android:text="Cancel"
        android:id="@+id/btn_row_option_cancel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

然后按鈕變得比默認大小更大,即使我沒有使寬度或高度更大。

這是截圖

如您所見,取消按鈕變大了。 其實我正在設置背景顏色。 即使設置了背景顏色,如何修復它以獲得默認大小?

在此處輸入圖片說明

您必須區分Button的大小、其寬度和高度、您可以單擊的整個區域以及用於顯示按鈕的圖像

android 上的默認按鈕對其可繪制對象應用了填充 - 看起來它更小。 它實際上具有相同的大小。

如果您制作自己的可繪制對象並且不應用填充,背景將在視圖的整個邊界內繪制,使其看起來更大。

您始終可以只使用默認按鈕,但使用 AppCompat 應用您自己的顏色。 這支持為默認按鈕的背景着色。

<android.support.v7.widget.AppCompatButton
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:backgroundTint="#ffaa00"/> <!-- <--- Your color here  -->

...這將讓您使用相同的外觀。

在 XML <Button>標簽中使用android:backgroundTint屬性。

喜歡:

android:backgroundTint="#6567dc"

無需使用<android.support.v7.widget.AppCompatButton>按鈕。

對於希望在代碼中執行此操作的人(不在 XML 中),您可以使用以下命令:

button.getBackground().setColorFilter(button.getContext().getResources().getColor(R.color.colorAccent), PorterDuff.Mode.MULTIPLY);

這並沒有改變按鈕的大小,並且也適用於舊的 android 版本。 這里討論了幾種方法,但這個方法很完美。

這是因為您使用了寬度和高度的換行內容。

您有多種選擇。 快速簡單的方法? 設置按鈕的值,指定 DP 中的大小,例如:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
        android:background="@color/white"
        android:layout_gravity="center_horizontal"
        android:id="@+id/btn_row_option_done"
        android:text="Done"
        android:layout_width="200dp"
        android:layout_height="200dp" />

    <Button
        android:background="@color/white"
        android:layout_gravity="center_horizontal"
        android:text="Edit"
        android:id="@+id/btn_row_option_edit"
        android:layout_width="200dp"
        android:layout_height="200dp" />

    <Button
        android:layout_gravity="center_horizontal"
        android:text="Delete"
        android:id="@+id/btn_row_option_delete"
        android:background="@color/red"
        android:layout_width="200dp"
        android:layout_height="200dp" />

    <Button
        android:layout_gravity="center_horizontal"
        android:text="Cancel"
        android:id="@+id/btn_row_option_cancel"
        android:layout_width="100dp"
        android:layout_height="100dp" />
</LinearLayout>

如果您想支持 API 級別 21 以下的任何內容,另一種選擇是創建一個自定義可繪制對象,並在彩色形狀周圍使用透明筆觸。 這使您還可以靈活地添加塊顏色和漸變。

注意縮小按鈕大小的stroke和透明度

button_background.xml

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

    <corners android:radius="5dp" />
    <padding
        android:left="10dp"
        android:right="10dp"
        android:top="0dp"
        android:bottom="0dp"/>

    <!-- This is where the magic happens -->
    <stroke android:color="#00FF0000" android:width="10dp"/>

    <solid android:color="#000000" />
</shape>

按鈕

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/button_background"
    android:text="Click Me"/>

嘗試使用此屬性設置按鈕顏色

button.setBackgroundTintList(ColorStateList.valueOf(Color.GREEN));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM