带有左右边距的Android按钮

8
我有以下布局。
<?xml version="1.0" encoding="utf-8"?>

<Button
    android:id="@+id/sign_in"
    style="@style/ButtonText"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/custom_button_green"
    android:text="@string/signin_via_email"
    android:textSize="15sp"
    android:typeface="sans" />

现在我并不希望按钮占满整个宽度,而是想要左边和右边各有10dp的边距。我已经设置了padding和layout_marginLeft及right属性,但它们似乎不起作用。请问有什么帮助吗?

谢谢!


1
你能发布你的 ButtonText 样式代码吗? - Chirag
android:layout_width="wrap_content"并相应地指定边距。 - neevek
4个回答

17
 <Button
 style="@style/ButtonText"
 android:layout_marginLeft="10dp"
 android:layout_marginRight="10dp"
 android:id="@+id/btnOk"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:text="OK" />

如果您使用“android:layout_marginHorizontal =”10dp“,它也可以工作。请注意,layout_marginHorizontal仅适用于API 26或更高版本! - Lotan
请使用layout_marginStartlayout_marginEnd - Eric

2
我添加了。
android:padding="10dp"

为了

input.xml

......然后它就可以工作了!

(我使用了SoftKeyboard源代码,也遇到了同样的问题)


android:layout_width="fill_parent" - Vừng Nhỏ

0

使用这个布局,它会为您的按钮添加边距。如果按钮是您的根元素,则无法从该布局中添加边距,但在将按钮添加到其他布局时,您可以指定边距/填充。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="10dip"
android:paddingRight="10dip" >
<Button
    android:id="@+id/sign_in"
    style="@style/ButtonText"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/custom_button_green"
    android:text="@string/signin_via_email"
    android:textSize="15sp"
    android:typeface="sans" />
</LinearLayout>

0

你好用户,

请不要在高度上使用"fill_parent"。请提供实际的图像高度和宽度,然后设置左右边距。这样肯定会起作用。


但我能计算屏幕的宽度吗?我只想在屏幕左右两侧有10 dp的边距。这应该可行吧? - AndroidDev

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接