带向右箭头的按钮
Button with right arrow
我正在尝试制作一个带有向右箭头的按钮。
我定义了一个卡片视图,里面有一个按钮。但我不确定如何将箭头与它放在一起,以及是否有可能在 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>
您可以通过 xml 中的 android:drawableRight
实现此目的,仅供参考,此 属性 也可以通过编程方式分配给视图,因为您必须使用 setCompoundDrawablesWithIntrinsicBounds(int,int,int,int)
这里的参数是
setCompoundDrawablesWithIntrinsicBounds(int /*FOR LEFT DRAWABLE*/,int/*FOR TOP DRAWABLE*/,int/*FOR RIGHT DRAWABLE*/,int/*FOR BOTTOM DRAWABLE*/)
如果你不想设置 drawable
然后将 0
传递给它
所以知道你只是在 xml 中使用第一个方法使用 android:drawableRight
属性
试试这个代码:
您可以通过右键单击 Drawable > New > Vector Asset > 剪贴画
<Button
android:id="@+id/signup"
...
android:drawableRight="your image"
...
android:textColor="#DE000000" />
尝试使用这种布局。
它适用于您的情况。
<?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>
您可以使用按钮 drawable right 或 drawable end 属性 来设置图像:
此外,您可以使用填充 属性 将填充应用于可绘制图像。
<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" />
伙计,你可以走了,
<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"/>
这很简单,你可以
首先,您需要一个矢量图标,您可以从官方 google 网站获得该图标
Material Icons.
然后你会得到一个 rar,同时你可以将每种类型的 drawable 解压到你的 android 项目的 mipmap 文件夹中
您将从 rar 文件中获得的文件是
drawable-hdpi
,drawable-mdpi
,drawable-xhdpi
,drawable-xxhdpi
drawable-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"/>
<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>
我正在尝试制作一个带有向右箭头的按钮。 我定义了一个卡片视图,里面有一个按钮。但我不确定如何将箭头与它放在一起,以及是否有可能在 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>
您可以通过 xml 中的 android:drawableRight
实现此目的,仅供参考,此 属性 也可以通过编程方式分配给视图,因为您必须使用 setCompoundDrawablesWithIntrinsicBounds(int,int,int,int)
这里的参数是
setCompoundDrawablesWithIntrinsicBounds(int /*FOR LEFT DRAWABLE*/,int/*FOR TOP DRAWABLE*/,int/*FOR RIGHT DRAWABLE*/,int/*FOR BOTTOM DRAWABLE*/)
如果你不想设置 drawable
然后将 0
传递给它
所以知道你只是在 xml 中使用第一个方法使用 android:drawableRight
属性
试试这个代码: 您可以通过右键单击 Drawable > New > Vector Asset > 剪贴画
<Button
android:id="@+id/signup"
...
android:drawableRight="your image"
...
android:textColor="#DE000000" />
尝试使用这种布局。 它适用于您的情况。
<?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>
您可以使用按钮 drawable right 或 drawable end 属性 来设置图像: 此外,您可以使用填充 属性 将填充应用于可绘制图像。
<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" />
伙计,你可以走了,
<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"/>
这很简单,你可以 首先,您需要一个矢量图标,您可以从官方 google 网站获得该图标 Material Icons.
然后你会得到一个 rar,同时你可以将每种类型的 drawable 解压到你的 android 项目的 mipmap 文件夹中
您将从 rar 文件中获得的文件是
drawable-hdpi ,drawable-mdpi ,drawable-xhdpi ,drawable-xxhdpi drawable-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"/>
<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>