带有左右边距的 Android 按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12405348/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 10:34:28  来源:igfitidea点击:

Android Button with Left and Right Margin

androidlayout

提问by AndroidDev

I have the following layout

我有以下布局

<?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" />

Now the button i dont really want to fit the whole width rather i want a margin of 10dp from the left and 10dp from the right. I have set the padding and layout_marginLeft and right attributes but they dont work. Any help ?

现在,我真的不想让按钮适合整个宽度,而是希望左侧有 10dp 的边距,右侧有 10dp 的边距。我已经设置了 padding 和 layout_marginLeft 和 right 属性,但它们不起作用。有什么帮助吗?

Kind Regards

亲切的问候

回答by MAC

 <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" />

回答by V??ng Nho?

I added

我加了

android:padding="10dp"

to

input.xml

..and it worked!

..它奏效了!

(I using SoftKeyboard source code and also have the same problem)

(我使用的是SoftKeyboard源代码,也有同样的问题)

回答by AndroidDanger

Hello User,

用户您好,

Please don't use "fill_parent" on Height ever. Please Give Actual image Height and Weight and Then Put MarginLeft and Right . It will work surely .

请不要在 Height 上使用“fill_parent”。请给出实际图像的高度和重量,然后将 MarginLeft 和 Right 放在一起。它肯定会起作用。

回答by Vishal Pawar

Use this layout it will add margin to your button if Button is your root element then there is no way to add margin from that layout while adding button to some other layout you can specify margin/padding

如果 Button 是您的根元素,则使用此布局将为您的按钮添加边距,则无法从该布局添加边距,同时将按钮添加到其他一些布局,您可以指定边距/填充

<?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>