我知道这是一件小事,但我不知道为什么它不起作用。
当我放置自定义可绘制图像时,它不起作用。我想要一个带有角(笔触)的透明背景按钮。
下面是图像。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/welcome"
tools:context=".MainActivity">
<ImageView
android:id="@+id/app_logo"
android:layout_width="300dp"
android:layout_height="100dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="60dp"
android:src="@drawable/applogo"/>
<TextView
android:id="@+id/app_slogan"
android:layout_width="220dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:layout_marginEnd="80dp"
android:layout_marginTop="140dp"
android:text="Find, Discover, Choose and buy anything online"
android:textAlignment="center"
android:textSize="22sp"
android:textStyle="bold|italic"
android:textColor="@color/design_default_color_primary"
/>
<Button
android:id="@+id/main_login_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="10dp"
android:layout_marginStart="15dp"
android:layout_marginEnd="15dp"
android:background="@drawable/buttons"
android:padding="20dp"
android:textAllCaps="false"
android:textSize="18sp"
android:text="Already have a account ? Login"
android:textColor="@android:color/white"
/>
<Button
android:id="@+id/main_join_now_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_above="@id/main_login_btn"
android:layout_marginStart="15dp"
android:layout_marginEnd="15dp"
android:background="@drawable/input_design"
android:padding="20dp"
android:textAllCaps="false"
android:textSize="18sp"
android:text="Already have a account ? Login"
android:textColor="@android:color/white"
/>
</RelativeLayout>
//可绘制的资源文件。
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke
android:color="#fff"
android:width="3dp"
/>
<corners
android:radius="15dp"
/>
登录按钮应该具有input_design.xml的背景,但是我不知道为什么它不起作用。帮帮我,谢谢你。
要获得按钮上的透明背景,请使用Widget.MaterialComponents.Button.OutlinedButton。
你将在build.gradle中需要此行(如果尚不存在):
implementation 'com.google.android.material:material:1.2.1'
然后,你的按钮将看起来像这样:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/teal_700"
tools:context=".FirstFragment">
<Button
android:id="@+id/button"
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="32dp"
android:text="I am a Button!"
android:textColor="@color/white"
app:cornerRadius="15dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:rippleColor="@color/white"
app:strokeColor="@color/white"
app:strokeWidth="3dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
注意:
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
android:textColor="@color/white"
app:cornerRadius="15dp"
app:rippleColor="@color/white"
app:strokeColor="@color/white"
app:strokeWidth="3dp"
这将产生:
请改用材料概述的按钮组件查看我的更新答案。如果不使用任何额外的xml形状等,这将是实现您正在寻找的目标的首选解决方案。
谢谢,这对我有用..但是告诉我一个最新的更新后的android studio精致的暗模式(主题)和亮模式主题可以实现,但是为什么在我创建和使用时不绘制资源文件作为背景,为什么它不起作用..帮助我
您是在问为什么我刚才回答的问题不能按您希望的方式起作用?如果是这样,我不确定,但是好像并没有正确应用xml且您的主题正在接管,这两种方式都可能是新的方式,可能是更好的方式,无济于事。观点。如果要全面执行此操作,则可以在主题中创建一种样式,并将其应用于该样式的所有按钮。
它对我有用。早先我使用了创建可绘制资源并将其作为背景,但是现在在新的AS更新之后,它不起作用了……但是您的方法对我有用,谢谢
是的,我不知道为什么它停止工作可能与主题和新组件有关,但是通常将旧的工作方式与更新的工作(如Android材料)混合会导致工作无法按预期进行。在我看来,旧方法也是胡扯。您可以更快地完成所需的工作,而无需任何多余的xml,而这些xml最终会使项目肿并四处散布。仅供参考,我尝试使用您的旧方法,但无法使用它,因此我采用了我在一个项目中使用的另一种方法。