图标下方带有文字的按钮(Android)

时间:2019-01-25 07:09:22

标签: android android-layout button

我正在尝试创建一个按钮,例如:image。 我刚刚尝试制作一个简单的按钮并使用属性android:drawableTop="@drawable/ic_my_icon,但是通过这种方式,我无法更改图标的大小:(

在我的项目中,我使用数据绑定和约束布局,在此片段中,我必须使用5个以上类似的按钮。我发现更好的方法是使用带有内部RelativeLayout的MaterialCardView。

<com.google.android.material.card.MaterialCardView
    android:id="@+id/cardTranslationTraining"
    android:layout_width="@dimen/cardSize"
    android:layout_height="@dimen/cardSize"
    app:cardBackgroundColor="@drawable/card_color_selector">  <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
            android:layout_width="60sp"
            android:layout_height="60sp"
            android:layout_centerHorizontal="true"
            android:layout_margin="5sp"
            app:srcCompat="@drawable/icon" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10sp"
            android:gravity="center"
            android:text="Translate" />

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="3dp"
            android:layout_alignParentBottom="true"
            android:background="@color/colorPrimaryDark" />
    </RelativeLayout>
</com.google.android.material.card.MaterialCardView>

但是有200多个字符串,看起来很糟糕!

不使用大嵌套怎么办?有人可以帮忙吗?
非常感谢您的帮助:)

2 个答案:

答案 0 :(得分:1)

尝试下面的布局代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:background="#DDDDDD">
<android.support.v7.widget.CardView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="20dp"
    app:cardMaxElevation="2dp"
    app:cardElevation="2dp"
    android:translationZ="2dp">
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="20dp">
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_like"/>
        <TextView
            android:layout_marginTop="10dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Like"/>
    </LinearLayout>
</android.support.v7.widget.CardView>

</LinearLayout>

这里的输出代码是:

enter image description here

我希望它对您有用。

答案 1 :(得分:0)

只需创建一个custom view component并一次使用相同的布局即可。并将您的自定义视图放入xml布局,如下所示。您还可以设置自定义属性,例如按钮图片来源和文字

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
    xmlns:custom="http://schemas.android.com/apk/res/com.example.customviews"> 

    <com.example.customviews.YourCustomView
     custom:showText="true"
     custom:labelPosition="left" /> 

    <com.example.customviews.YourCustomView
     custom:showText="true"
     custom:labelPosition="left" /> 

    <com.example.customviews.YourCustomView
     custom:showText="true"
     custom:labelPosition="left" /> 
</LinearLayout>

Custom View Component Source

Custom View Class