[英]Android: How to make the text size of Borderless button same as that of TextView
[英]Make borderless image button small on Android
我的應用程序中有一個可關閉的視圖,並且標題中有一個關閉按鈕。 我想使此按鈕無邊界且小。
編碼:
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="?android:attr/borderlessButtonStyle"
android:layout_margin="0dp"
android:padding="0dp"
android:src="@android:drawable/ic_menu_close_clear_cancel"/>
您應該使用:
android:background="?selectableItemBackgroundBorderless"
您可以在ImageButton上使用以下命令刪除“邊框”:
android:background="@android:color/transparent"
要么
android:background="@null"
如果您想在用戶單擊按鈕時更改背景,則可以創建一個選擇器。 示例ImageButton在具有透明背景的點擊中不會突出顯示 。
通過引入ConstraintLayout
,您可以使用約束來減小無邊界ImageButton
的寬度(默認情況下過寬),方法是將其限制為與高度一樣寬:
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/my_closeable_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageButton
style="?android:attr/borderlessButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:contentDescription="@string/action_close"
android:src="@android:drawable/ic_menu_close_clear_cancel"
app:layout_constraintDimensionRatio="w,1:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
我不建議您將其設置為小於此值,因為根據Google的材料設計指南 ,觸摸目標必須至少為48dp x 48dp,否則用戶將很難觸摸它。
如果確實需要使其看起來更接近視圖的邊緣,則可以始終使用不居中的圖像(在一側或兩側具有透明填充),但是我會盡力避免這種變通方法,並且請嘗試重新考慮我的應用程序的設計,以便按原樣容納按鈕。
為此使用textView。並使用textview.OnTouchListener最終onTouch更改文本的顏色。
否則在您的代碼中只使用
android:background="@null"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.