带有图标和文本的按钮

时间:2016-05-09 10:31:06

标签: android button layout

我想在Android中制作一个Button

Connexion button

但我不知道该怎么做。 我是否必须使用RelativeLayout一些元素(ImageViewTextView,分隔符,...)?

我真的不知道最佳做法。

5 个答案:

答案 0 :(得分:5)

  

您可以将Button与属性android:drawableLeft="@drawable/ic_done"一起使用。

请参阅此内容。

 <Button
        android:id="@+id/button111111"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:drawablePadding="5dp"
        android:layout_centerHorizontal="true"
        android:drawableLeft="@drawable/ic_done"
        android:text="Facebook" />

编辑1:

  

如果您想在不使用Button财产android:drawableLeft="@drawable/ic_done"的情况下执行此操作。

请参阅此内容。

XML文件夹中创建名为Shape.xml的{​​{1}}文件。

如果您在res目录中看不到drawable文件夹,则创建名为drawable的文件夹。

res -> drawable

然后参考此布局。

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <stroke
        android:width="3dip"
        android:color="@android:color/black" />
    <corners android:radius="10dip" />
    <padding
        android:bottom="10dip"
        android:left="10dip"
        android:right="10dip"
        android:top="10dip" />
</shape>

这是屏幕。

enter image description here

注意:根据您的选择更改<?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="wrap_content" android:background="@drawable/shape" android:gravity="center_vertical"> <ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="@string/app_name" android:src="@mipmap/ic_launcher" /> <View android:layout_width="2dp" android:layout_height="match_parent" android:layout_marginLeft="10dp" android:background="@android:color/darker_gray" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_weight="1" android:background="@null" android:text="Submit" /> </LinearLayout> Drawable

答案 1 :(得分:2)

我为你做了。保存图像并设置为按钮的背景

i

答案 2 :(得分:1)

您只需使用onBackPressed()并使用back属性设置您想要的图标。

请参阅文档:https://developer.android.com/guide/topics/ui/controls/button.html

答案 3 :(得分:1)

使用带有文本和图标的Button类,使用带有 android:drawableLeft 属性的Button类:

试试这个:

<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text Here"
android:drawableLeft="@drawable/your_image"
 />

答案 4 :(得分:0)

编辑图像以删除边框周围的额外区域并将图像设置为背景