【问题标题】:Button with icon and text带有图标和文本的按钮
【发布时间】:2016-09-03 22:54:08
【问题描述】:

我想在 Android 中创建一个类似的Button

但我不知道该怎么做。 我是否必须使用带有某些元素的RelativeLayoutImageViewTextView、分隔符...)?

我真的不知道最佳实践。

【问题讨论】:

    标签: android button layout


    【解决方案1】:

    您可以将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 -&gt; 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>
    

    这是屏幕。

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 谢谢,但我需要翻译文本。这对我来说不是一个好的解决方案。
      【解决方案3】:

      带有文本和图标,使用带有 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"
       />
      

      【讨论】:

      • 谢谢!我现在在左边有drawable。但是我怎样才能得到垂直分隔符?
      • @Jewom 你检查我的答案了吗?
      • @Jewom 检查下面我更新的EDIT 1 部分。它会给你你想要的。
      【解决方案4】:

      您可以只使用&lt;Button&gt; 并设置您想要使用的图标,即android:drawableLeft 属性。

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

      【讨论】:

        【解决方案5】:

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

        【讨论】:

          猜你喜欢
          • 2014-05-22
          • 2020-08-30
          • 2018-09-01
          • 2019-10-27
          • 1970-01-01
          • 2023-01-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多