带图标和文本的按钮

7
我想制作一个类似于Android上的按钮:Button

Connexion button

但我不知道该怎么做。 我需要使用RelativeLayout和一些元素(ImageViewTextView,分隔符等)吗?

我真的不知道最佳实践。

5个回答

7
你可以使用带有属性 android:drawableLeft="@drawable/ic_done"Button

请参阅此处。
 <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" />

EDIT 1:

如果您不想使用属性 android:drawableLeft="@drawable/ic_done"Button 进行操作,则可以按照以下步骤操作。

请参考以下步骤:

res -> drawable 文件夹中创建名为 Shape.xmlXML 文件。

如果您在 res 目录中没有看到 drawable 文件夹,则需要创建一个名为 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>

请参考这个布局。
<?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>

这是屏幕截图。

进入图片描述

注意:根据您的选择更改DrawableColor


3
我已经为您完成了此任务。请保存该图像并设置为按钮的背景。

i


谢谢,但我需要翻译文本。这对我来说不是好的解决方案。 - user6299584

2

使用Button类和android:drawableLeft属性来显示文本和图标:

尝试这样做:

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

谢谢!现在我已经把可绘制对象放在了左边。但是我怎么才能得到垂直分隔符呢? - user6299584
@Jewom 你有检查过我的答案吗? - Jay Rathod
@Jewom 请查看我下面更新的 EDIT 1 部分。它会给你想要的东西。 - Jay Rathod

1

0

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


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