【问题标题】:Button with right arrow带右箭头的按钮
【发布时间】:2018-11-09 23:17:01
【问题描述】:

我正在尝试制作一个带有右箭头的按钮。 我已经定义了一个卡片视图,里面有一个按钮。但我不确定如何将箭头与它一起放置,以及是否可以在 xml 中实现。 按钮应如下所示:

我的 xml 看起来像这样:

 <android.support.v7.widget.CardView
    android:id="@+id/signupCard"
    style="@style/CardView.Light"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginBottom="18dp"
    android:layout_marginEnd="29dp"
    android:layout_marginLeft="29dp"
    android:layout_marginRight="29dp"
    android:layout_marginStart="29dp"
    android:layout_marginTop="198dp"
    app:layout_constraintBottom_toTopOf="@+id/loginCard"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <Button
        android:id="@+id/signup"
        style="@style/Widget.AppCompat.Button.Borderless"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/background_light"
        android:minWidth="0dp"
        android:paddingEnd="8dp"
        android:paddingStart="8dp"
        android:scaleType="center"
        android:text="@string/signUp"
        android:textColor="#DE000000" />
</android.support.v7.widget.CardView>

【问题讨论】:

    标签: android xml button material-design android-cardview


    【解决方案1】:

    尝试使用此布局。 它适用于您的情况。

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout 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">
    
    <androidx.cardview.widget.CardView
        android:id="@+id/signupCard"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="18dp"
        android:layout_marginEnd="29dp"
        android:layout_marginLeft="29dp"
        android:layout_marginRight="29dp"
        android:layout_marginStart="29dp"
        android:layout_marginTop="198dp"
        app:cardCornerRadius="@dimen/d5dp"
        app:cardElevation="@dimen/d10dp">
    <Button
            android:id="@+id/signup"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_margin="@dimen/d50dp"
            android:background="@android:color/background_light"
            android:drawablePadding="@dimen/d30dp"
            android:drawableEnd="@drawable/icon_right_aero_black"
            android:minWidth="0dp"
            android:scaleType="center"
            android:text="Login"
            android:textColor="#DE000000" />
    </androidx.cardview.widget.CardView>
    </RelativeLayout>
    

    【讨论】:

    • 对于 FontFamily,您可以在 Button 的 .xml 布局中添加 android:fontFamily="" 属性并设置 android:textAllCaps="false"。
    • 完美答案。
    【解决方案2】:

    试试这个代码: 您可以通过右键单击 Drawable > New > Vector Asset > Clip Art 获得该图像强>

    <Button
        android:id="@+id/signup"
        ...
        android:drawableRight="your image"
        ...
        android:textColor="#DE000000" />
    

    【讨论】:

      【解决方案3】:

      您可以通过android:drawableRight 在 xml 中实现这一点,并且仅供参考,此属性也可以通过编程方式分配给视图,因为您必须使用 setCompoundDrawablesWithIntrinsicBounds(int,int,int,int)
      这里的参数是

      setCompoundDrawablesWithIntrinsicBounds(int /*FOR LEFT DRAWABLE*/,int/*FOR TOP DRAWABLE*/,int/*FOR RIGHT DRAWABLE*/,int/*FOR BOTTOM DRAWABLE*/)

      如果您不想设置drawable,则将0 传递给它

      所以要知道您只需使用android:drawableRight 属性在 xml 中使用第一种方法

      【讨论】:

        【解决方案4】:

        您可以使用按钮drawable right或drawable end属性来设置图像: 此外,您可以使用 padding 属性将填充应用于可绘制图像。

        <Button
              android:id="@+id/imgButton"
              android:text="imageButton"
              android:drawableRight="@drawable/your_image"
              android:drawableEnd="@drawable/your_image"
              android:drawablePadding="@dimen/padding_10dp"
              android:textColor="@color/colorButton" />
        

        【讨论】:

          【解决方案5】:

          你可以去,伙计,

          <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:card_view="http://schemas.android.com/apk/res-auto"
          xmlns:tools="http://schemas.android.com/tools"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          card_view:cardCornerRadius="5dp"
          card_view:cardElevation="3dp"
          android:layout_marginBottom="18dp"
          android:layout_marginEnd="29dp"
          android:layout_marginLeft="29dp"
          android:layout_marginRight="29dp"
          android:layout_marginStart="29dp"
          android:layout_marginTop="198dp"
          card_view:cardPreventCornerOverlap="false"
          card_view:cardUseCompatPadding="true"
          >
          <Button
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Login"
              android:background="@color/white"
              android:drawableRight="@drawable/ic_chevron"/>
          

          【讨论】:

            【解决方案6】:

            很简单,你可以 首先你需要一个矢量图标,你可以从谷歌官方网站获得 Material Icons.

            然后你会得到一个 rar,你可以将每种类型的 drawable 提取到你的 android 项目的 mipmap 文件夹中

            您将从 rar 文件中获得的文件是

            可绘制的 hdpi ,drawable-mdpi ,drawable-xhdpi ,drawable-xxhdpi 可绘制-xxxhdpi

            将包含在此文件夹中的图像分别复制到 mipmap-hdpi、mipmap-mdpi、mipmap-xhdpi、mipmap-xxhdpi 和 mipmap-xxxhdpi 中

            现在你已经有了图标,现在可以关注

            <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Login"
            android:background="@color/white"
            android:drawableRight="@mipmap/yourIconName"/>
            

            【讨论】:

              【解决方案7】:
                       <android.support.v7.widget.CardView 
                                    xmlns:android="http://schemas.android.com/apk/res/android"
                                      xmlns:card_view="http://schemas.android.com/apk/res- 
                                       auto"
                                      android:id="@+id/cardview1"
                                      android:layout_width="fill_parent"
                                      android:layout_height="wrap_content"
                                      android:layout_margin="12dp"
                                      android:layout_marginRight="7dp"
                                      android:layout_marginTop="25dp"
                                      android:clickable="true"
                                      android:foreground="? 
                                      android:attr/selectableItemBackground"
                                      card_view:cardBackgroundColor="@color/White"
                                      card_view:cardCornerRadius="7dp"
                                      card_view:cardElevation="7dp"
                                      card_view:cardMaxElevation="7dp"
                                      card_view:contentPadding="7dp">
              
                                      <RelativeLayout
                                          android:layout_width="fill_parent"
                                          android:layout_height="wrap_content"
                                          android:orientation="horizontal"
                                          android:padding="5dp">
              
              
              
                                          <TextView
                                              style="@style/TextColor"
                                              android:layout_width="wrap_content"
                                              android:layout_height="wrap_content"
                                              android:layout_alignParentStart="true"
                                              android:layout_centerVertical="true"
                                              android:layout_gravity="center"
                                              android:layout_marginStart="64dp"
                                              android:paddingLeft="10dp"
              
                                              android:text="@string/MySchool"
                                              android:textSize="20dp"
                                              tools:ignore="RtlCompat" />
              
                                          <ImageButton
                                              android:layout_width="wrap_content"
                                              android:layout_height="wrap_content"
                                              android:layout_alignParentEnd="true"
                                              android:layout_centerVertical="true"
              
                                              android:layout_marginEnd="24dp"
                                              android:background="@color/tranbackground"
              
                                  android:src="@drawable/ic_keyboard_arrow_right_black_24dp"
                                              tools:ignore="RtlCompat" />
                                      </RelativeLayout>
                                  </android.support.v7.widget.CardView>
              

              【讨论】:

              • 没有解释的代码不是很有帮助 - 你能详细说明吗?