有没有一种好方法可以让 Android 中的按钮根据您是单击按钮的左半部分还是右半部分来执行不同的操作?
Is there a good way to make a button in Android do different things depending on if you click on the left half or right half of the button?
在我制作的 android 应用程序中,我想制作一个按钮,根据用户是按下按钮的左半部分还是按钮的右半部分来执行不同的操作。现在我正在尝试找出实现此目标的最佳方法。
其他一些具体要求:
1. 我计划每个 Activity.
使用按钮 1 到 4 次
2. 如果我能够旋转按钮(例如 90 度、180 度,所以它是倒置的),那将非常有帮助
我的一个想法是并排放置两个按钮,然后在顶部放置一个文本视图,使其看起来像一个按钮。我发现这并不能很好地工作。它需要大量的努力才能正确显示,即使进行很小的更改也会变得混乱。
我的另一个想法是通过扩展视图制作自定义按钮 class。问题是我没有做类似事情的经验,而且我看到的大多数教程都使用它来制作绘画程序。
创建这样的东西的最佳方法是什么?
编辑:当我说旋转按钮时,我并不是说按钮在单击或执行其他操作时需要旋转。只是它朝向应用程序加载时我需要的方向。此外,它只需要面向标准的 4 个方向(即下、上、左、右)。抱歉,我不是很清楚。
我使用框架布局创建了第一种方法,如下所示:
<FrameLayout
android:id="@+id/frame"
android:layout_width="match_parent"
android:layout_height="70dp"
android:onClick="rotate">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
app:layout_constraintBottom_toTopOf="@+id/view"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView2">
<Button
android:id="@+id/button"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@color/colorPrimary" />
<Button
android:id="@+id/button2"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@color/colorAccent" />
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:text="Button text"
android:textAppearance="@style/TextAppearance.AppCompat.Display1"
android:textColor="@android:color/white" />
</FrameLayout>
要旋转任何视图,请在 res
的 anim
文件夹中创建 anim.xml 文件
res
上的 right click
-> new android res dir
-> 选择 anim
然后创建动画文件 右键单击刚创建的 anim
文件夹和 new anim res file
以及通过它里面的代码,这将把你的框架布局在这个特定的代码中旋转 180 度
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true">
<rotate
android:duration="500"
android:fromDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="180" />
</set>
像 kotlin 一样使用它
fun rotate(view: View) {
val rotate = AnimationUtils.loadAnimation(
applicationContext,
R.anim.rotate
)
frame.startAnimation(rotate)
}
在我制作的 android 应用程序中,我想制作一个按钮,根据用户是按下按钮的左半部分还是按钮的右半部分来执行不同的操作。现在我正在尝试找出实现此目标的最佳方法。
其他一些具体要求:
1. 我计划每个 Activity.
使用按钮 1 到 4 次
2. 如果我能够旋转按钮(例如 90 度、180 度,所以它是倒置的),那将非常有帮助
我的一个想法是并排放置两个按钮,然后在顶部放置一个文本视图,使其看起来像一个按钮。我发现这并不能很好地工作。它需要大量的努力才能正确显示,即使进行很小的更改也会变得混乱。
我的另一个想法是通过扩展视图制作自定义按钮 class。问题是我没有做类似事情的经验,而且我看到的大多数教程都使用它来制作绘画程序。
创建这样的东西的最佳方法是什么?
编辑:当我说旋转按钮时,我并不是说按钮在单击或执行其他操作时需要旋转。只是它朝向应用程序加载时我需要的方向。此外,它只需要面向标准的 4 个方向(即下、上、左、右)。抱歉,我不是很清楚。
我使用框架布局创建了第一种方法,如下所示:
<FrameLayout
android:id="@+id/frame"
android:layout_width="match_parent"
android:layout_height="70dp"
android:onClick="rotate">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
app:layout_constraintBottom_toTopOf="@+id/view"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView2">
<Button
android:id="@+id/button"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@color/colorPrimary" />
<Button
android:id="@+id/button2"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@color/colorAccent" />
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:text="Button text"
android:textAppearance="@style/TextAppearance.AppCompat.Display1"
android:textColor="@android:color/white" />
</FrameLayout>
要旋转任何视图,请在 res
的 anim
文件夹中创建 anim.xml 文件
res
上的 right click
-> new android res dir
-> 选择 anim
然后创建动画文件 右键单击刚创建的 anim
文件夹和 new anim res file
以及通过它里面的代码,这将把你的框架布局在这个特定的代码中旋转 180 度
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true">
<rotate
android:duration="500"
android:fromDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="180" />
</set>
像 kotlin 一样使用它
fun rotate(view: View) {
val rotate = AnimationUtils.loadAnimation(
applicationContext,
R.anim.rotate
)
frame.startAnimation(rotate)
}