问题
I am using Button
in one of the layouts. I want Button
size to be as small as the text/label size.
I am using the following code.
<Button
android:text="@string/cancle_button"
android:id="@+id/button1"
android:background="@drawable/back_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="12sp"
android:textColor="#ffffff"
android:textStyle="bold" />
But I want Button
height to be same as text/label height. Whenever I decrease or increase the text height, Button
height remains the same. How should I solve this problem?
回答1:
You can use a relative layout to achieve this. Check this sample code,
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="TextView" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/textView1"
android:layout_alignTop="@id/textView1"
android:layout_alignBottom="@id/textView1"
android:text="Button" />
</RelativeLayout>
回答2:
It's been almost a year since the question was asked. But I faced the problem now and got a solution. :) May be it will help someone.
You can achieve this by setting android:minHeight="0dp"
for the Button widget.
And I guess this behavior is because there is some default value (may be a 48dp) set for Button widgets by the framework considering a minimum touch area.
回答3:
Use can take the image view in this case or if your taking text view in this case so you can take the image with proper size so that it would not bluer through these measures you can set small size for image Best way is you can take the image with that size you want to create its good way
回答4:
You can solve this issue by setting the button height to a specific value which is same as the height of your label.
<Button
android:text="CANCEL"
android:id="@+id/button1"
android:background="@drawable/back_button"
android:layout_width="wrap_content"
android:layout_height="20sp"
android:gravity="center"
android:textSize="15sp"
android:textColor="#ffffff"
android:textStyle="bold" />
来源:https://stackoverflow.com/questions/19556799/android-decreasing-size-of-the-button