【发布时间】:2016-09-03 22:54:08
【问题描述】:
我想在 Android 中创建一个类似的Button:
但我不知道该怎么做。
我是否必须使用带有某些元素的RelativeLayout(ImageView、TextView、分隔符...)?
我真的不知道最佳实践。
【问题讨论】:
我想在 Android 中创建一个类似的Button:
但我不知道该怎么做。
我是否必须使用带有某些元素的RelativeLayout(ImageView、TextView、分隔符...)?
我真的不知道最佳实践。
【问题讨论】:
您可以将
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"的情况下这样做。
参考这个。
在res -> drawable 文件夹中创建XML 文件并命名为Shape.xml。
如果您在 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>
这是屏幕。
注意:根据您的选择更改您的Drawable 和Color。
【讨论】:
带有文本和图标,使用带有 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"
/>
【讨论】:
EDIT 1 部分。它会给你你想要的。
您可以只使用<Button> 并设置您想要使用的图标,即android:drawableLeft 属性。
请参阅文档:https://developer.android.com/guide/topics/ui/controls/button.html
【讨论】:
编辑图像以删除边框周围的多余区域并将图像设置为背景
【讨论】: