How do you remove the extra inner padding inside a Spinner button in an Android layout?

StackOverflow https://stackoverflow.com/questions/23452666

  •  15-07-2023
  •  | 
  •  

سؤال

I am trying to create a Button that looks like a Spinner.

I have done this by creating a Button like this:

    <Button
        android:id="@+id/dateButton"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/dateLabel"
        android:text="default text"
        android:textSize="15sp"
        style="?android:attr/spinnerStyle"
        android:gravity="center_vertical|center_horizontal" />  

The problem is that the text ("default text") inside the Button seems to have extra padding around it, which makes the Button look bloated and expanded in its height.

I thought that setting the layout_height for the button to "wrap_content" would fix this and make the button thinner, but it does not have any effect.

Does anyone know how to make this button's height wrap to the text inside it?

Here is the example code:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:layout_marginLeft="10dp" >

    <TextView
        android:id="@+id/dateLabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="25dp"
        android:text="Select date:"
        android:textSize="15sp" />

    <Button
        android:id="@+id/dateButton"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/dateLabel"
        android:text="default text"
        android:textSize="15sp"
        style="?android:attr/spinnerStyle"
        android:gravity="center_vertical|center_horizontal" />  

</RelativeLayout>
هل كانت مفيدة؟

المحلول

android:gravity="center_vertical|center_horizontal" instead of this you can write android:gravity="center" but as per your requirment just remove this line.do not add any gravity and change your height size android:layout_height="40dp" i changed your code

<Button
        android:id="@+id/dateButton"
        android:layout_width="150dp"
        android:layout_height="35dp"
        android:layout_toRightOf="@+id/dateLabel"
        android:text="default text"
        style="?android:attr/spinnerStyle"
         />

نصائح أخرى

The padding of this button is decided by the drawable of it. You either need to change the spinner drawable (can be done at runtime) , or provide your own drawable to the given button that would make it look like a spinner, without using the default one

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top