Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/179.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Android中创建中间有图标的按钮?_Android_Xml_Android Button_Material Components Android - Fatal编程技术网

如何在Android中创建中间有图标的按钮?

如何在Android中创建中间有图标的按钮?,android,xml,android-button,material-components-android,Android,Xml,Android Button,Material Components Android,我应该创建一个按钮,在按钮中心的文本旁边有一个图标,如下所示: 这就是我提出的解决方案: <androidx.cardview.widget.CardView android:layout_width="0dp" android:layout_height="75dp" android:layout_marginTop="10dp" android:layout_marginBottom=&quo

我应该创建一个按钮,在按钮中心的文本旁边有一个图标,如下所示:

这就是我提出的解决方案:

<androidx.cardview.widget.CardView
    android:layout_width="0dp"
    android:layout_height="75dp"
    android:layout_marginTop="10dp"
    android:layout_marginBottom="10dp"
    android:backgroundTint="@color/tomato"
    app:cardCornerRadius="70dp"

    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintWidth_percent="0.9">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingTop="20dp">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:orientation="horizontal">

            <ImageView
                android:layout_width="35dp"
                android:layout_height="35dp"
                android:layout_marginRight="10dp"
                android:src="@drawable/ic_baseline_shopping_cart_24" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Add This To Cart"
                android:textColor="@color/white"
                android:textSize="25dp" />
        </LinearLayout>
    </RelativeLayout>
</androidx.cardview.widget.CardView>


但我认为这是非常硬的编码。因此,我的问题是:有没有更好的方法来设计它或使用一些库来实现它?

您可能应该开始使用,它附带了大量预构建的现代组件,以避免自己实现像这样的琐碎事情

然后可以使用
MaterialButton
设置图标属性:

<com.google.android.material.button.MaterialButton
    app:icon="@drawable/..." />


您可以查看特定的,以获取样式设置等信息。

您可能应该开始使用,它附带了大量预构建的现代组件,以防止您自己实现像这样的琐碎事情

然后可以使用
MaterialButton
设置图标属性:

<com.google.android.material.button.MaterialButton
    app:icon="@drawable/..." />


有关样式设置等信息,您可以查看特定视图。

删除图像视图,然后 在文本视图中写这行

android:drawableLeft=“@drawable/ic\u baseline\u shopping\u cart\u 24

在你的代码中

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add This To Cart"
android:textColor="@color/white"
android:textSize="25dp" 
androidd:drawableLeft="@drawable/ic_baseline_shopping_cart_24/>

删除您的ImageView,然后
在文本视图中写这行

android:drawableLeft=“@drawable/ic\u baseline\u shopping\u cart\u 24

在你的代码中

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add This To Cart"
android:textColor="@color/white"
android:textSize="25dp" 
androidd:drawableLeft="@drawable/ic_baseline_shopping_cart_24/>

您可以使用
材料按钮

   <com.google.android.material.button.MaterialButton
       app:icon="@drawable/ic_add_24px"
       app:iconPadding="0dp"
       app:iconGravity="textStart"
       app:shapeAppearanceOverlay="@style/ShapeAppearanceOverlay.App.rounded50"
       ../>

与:


50%

您可以使用
材料按钮

   <com.google.android.material.button.MaterialButton
       app:icon="@drawable/ic_add_24px"
       app:iconPadding="0dp"
       app:iconGravity="textStart"
       app:shapeAppearanceOverlay="@style/ShapeAppearanceOverlay.App.rounded50"
       ../>

与:


50%

哦。我不知道我能做到。谢谢。我不知道我能做到。谢谢