Android 将符号放置在按钮的中心
我试图在按钮的中央设置“+”符号,但它在底部Android 将符号放置在按钮的中心,android,android-button,Android,Android Button,我试图在按钮的中央设置“+”符号,但它在底部 <Button android:background="@drawable/zoom_button_background" android:layout_height="40dp" android:layout_width="40dp" android:textSize="24sp" android:textColor="@color/black2" android
<Button android:background="@drawable/zoom_button_background"
android:layout_height="40dp"
android:layout_width="40dp"
android:textSize="24sp"
android:textColor="@color/black2"
android:text="+"
android:textStyle="bold"
android:gravity="center"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_marginTop="160dp"
android:layout_marginRight="10dp"/>
我想将textSize设置为42sp,但当我这样做时,plus不在中间。我怎样才能让他进入中心?试试这个:
<Button android:background="@drawable/zoom_button_background"
android:layout_height="40dp"
android:layout_width="40dp"
android:textSize="24sp"
android:textColor="@color/black2"
android:text="+"
android:textStyle="bold"
android:gravity="center_vertical|center_horizontal"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal|center" >
</button>
图像按钮将是最佳选择,因为背景与重力无关
<ImageButton android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/zoom_button_background" />
发生这种情况是因为您设置的文本大小(42sp)对于按钮的给定大小(40dp x 40dp)太大。不要明确设置按钮的大小,而是使用wrap\u content
来设置宽度和高度。请看我上面的评论。只需使用此属性并让我知道发生了什么。此解决方案不适用于更改版面宽度和版面高度以包装内容并运行,如果成功,请告诉我。不要忘记在项目上运行干净