如何在android xml中设计4个可点击的圆形字段
Posted
技术标签:
【中文标题】如何在android xml中设计4个可点击的圆形字段【英文标题】:How to design 4 clickable circular fields in android xml 【发布时间】:2020-07-09 06:16:04 【问题描述】:这应该是一个非常简单有趣的游戏,您可以选择 2 种颜色,而您的朋友必须猜出您选择了哪一种。我基本上希望我的 Activity 看起来像这样:
每个季度都应该可以通过 onClick-Animation 进行点击(我猜如果它看起来像这样就不可能是按钮)
使用 XML 代码获得此结果的最佳方法是什么? 我在考虑一个带有 ImageViews 的 ConstraintLayout,但是我没有得到可点击的圆形,而是得到了一个可点击区域的矩形。
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_
android:layout_
android:orientation="vertical">
<ImageView
android:id="@+id/imgOne"
android:layout_
android:layout_
android:background="@color/colorPrimary"
app:layout_constraintBottom_toTopOf="@+id/imgThree"
app:layout_constraintEnd_toStartOf="@id/imgTwo"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:layout_
android:layout_
android:padding="10dp"
android:text="NEW ITEM"
android:textColor="@color/colorBlack"
app:layout_constraintBottom_toBottomOf="@id/imgOne"
app:layout_constraintEnd_toStartOf="@id/imgTwo"
app:layout_constraintStart_toStartOf="parent" />
<ImageView
android:id="@+id/imgTwo"
android:layout_
android:layout_
android:background="@color/colorblue"
app:layout_constraintBottom_toTopOf="@+id/imgFour"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@id/imgOne"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:layout_
android:layout_
android:padding="10dp"
android:text="NEW ITEM"
android:textColor="@color/colorBlack"
app:layout_constraintBottom_toBottomOf="@id/imgTwo"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@id/imgTwo" />
<ImageView
android:id="@+id/imgThree"
android:layout_
android:layout_
android:background="@color/colorGreen"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@id/imgTwo"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imgOne" />
<TextView
android:layout_
android:layout_
android:padding="10dp"
android:text="NEW ITEM"
android:textColor="@color/colorBlack"
app:layout_constraintBottom_toBottomOf="@id/imgThree"
app:layout_constraintEnd_toStartOf="@id/imgFour"
app:layout_constraintStart_toStartOf="parent" />
<ImageView
android:id="@+id/imgFour"
android:layout_
android:layout_
android:background="@color/colorAccent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@id/imgThree"
app:layout_constraintTop_toBottomOf="@+id/imgTwo" />
<TextView
android:layout_
android:layout_
android:padding="10dp"
android:text="NEW ITEM"
android:textColor="@color/colorBlack"
app:layout_constraintBottom_toBottomOf="@id/imgThree"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@id/imgTwo" />
</androidx.constraintlayout.widget.ConstraintLayout>
这可以通过毕加索创建一个圆形图像来完成,但这更像是一种解决方法,而不是解决方案。
【问题讨论】:
在下面查看我的答案,勾选或考虑接受;) 【参考方案1】:这是诀窍, 您可以仅使用 xml 布局来创建它
首先在drawable Folder中创建4个矢量路径形状
ic_top_left.xml
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:
android:
android:viewportWidth="250"
android:viewportHeight="250">
<group>
<clip-path
android:pathData="M0,0h250v250h-250z"/>
<path
android:pathData="M240,240L15,240a226.612,226.612 0,0 1,4.571 -45.346A223.763,223.763 0,0 1,53.426 114.2a225.659,225.659 0,0 1,98.993 -81.518,223.933 223.933,0 0,1 42.235,-13.11A226.68,226.68 0,0 1,240 15L240,240Z"
android:fillColor="#F8675D"/>
</group>
</vector>
ic_top_right.xml
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:
android:
android:viewportWidth="250"
android:viewportHeight="250">
<group>
<clip-path
android:pathData="M0,0h250v250h-250z"/>
<path
android:pathData="M10,15l-0,-0a226.76,226.76 135,0 1,45.344 4.572,223.929 223.929,0 0,1 42.235,13.11A225.077,225.077 0,0 1,135.8 53.427,226.632 226.632,135 0,1 169.1,80.9a226.592,226.592 135,0 1,27.474 33.3A224.976,224.976 135,0 1,217.318 152.421,223.788 223.788,45 0,1 230.428,194.655 226.613,226.613 0,0 1,235 240L10,240L10,15Z"
android:fillColor="#5699ff"/>
</group>
</vector>
ic_bott_left.xml
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:
android:
android:viewportWidth="250"
android:viewportHeight="250">
<group>
<clip-path
android:pathData="M0,0h250v250h-250z"/>
<path
android:pathData="M240,235h0a226.76,226.76 0,0 1,-45.344 -4.572,223.929 223.929,0 0,1 -42.235,-13.11A225.077,225.077 0,0 1,114.2 196.573,226.632 226.632,0 0,1 80.9,169.1a226.592,226.592 0,0 1,-27.474 -33.3A224.976,224.976 0,0 1,32.682 97.579,223.788 223.788,0 0,1 19.572,55.345 226.613,226.613 0,0 1,15 10L240,10L240,235Z"
android:fillColor="#8BC34A"/>
</group>
</vector>
ic_bott_right.xml
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:
android:
android:viewportWidth="250"
android:viewportHeight="250">
<group>
<clip-path
android:pathData="M0,0h250v250h-250z"/>
<path
android:pathData="M10,10L235,10a226.678,226.678 0,0 1,-4.571 45.346A223.763,223.763 0,0 1,196.574 135.8a225.654,225.654 0,0 1,-98.993 81.519,223.933 223.933,45 0,1 -42.235,13.11A226.676,226.676 0,0 1,10 235L10,10Z"
android:fillColor="#FFC107"/>
</group>
</vector>
然后在, activity_main.xml
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
tools:context=".MainActivity">
<TextView
android:layout_
android:layout_
android:layout_marginTop="50dp"
android:text="DashBoard"
android:layout_centerHorizontal="true"
android:textSize="25sp"
android:textStyle="bold"
android:textColor="@android:color/background_dark"/>
<RelativeLayout
android:layout_centerInParent="true"
android:layout_
android:layout_>
<!--Top_Left_Btn-->
<LinearLayout
android:onClick="OnClickAndroid"
android:layout_
android:layout_
android:paddingStart="15dp"
android:gravity="center"
android:orientation="vertical"
android:background="@drawable/ic_top_left">
<ImageView
android:layout_
android:layout_
android:layout_marginTop="32dp"
android:layout_marginBottom="10dp"
android:src="@drawable/ic_android"/>
<TextView
android:layout_
android:layout_
android:textColor="@android:color/white"
android:textSize="18sp"
android:text="Android"
android:textStyle="bold"/>
</LinearLayout>
<!--Top_Right_Btn-->
<LinearLayout
android:onClick="OnClickRide"
android:layout_
android:layout_
android:layout_alignParentEnd="true"
android:paddingEnd="15dp"
android:gravity="center"
android:orientation="vertical"
android:background="@drawable/ic_top_right">
<ImageView
android:layout_
android:layout_
android:layout_marginTop="32dp"
android:layout_marginBottom="10dp"
android:src="@drawable/ic_ride"/>
<TextView
android:layout_
android:layout_
android:textSize="18sp"
android:textColor="@android:color/white"
android:text="Ride"
android:textStyle="bold"/>
</LinearLayout>
<!--Bottom_Left_Btn-->
<LinearLayout
android:onClick="OnClickBugs"
android:layout_
android:layout_
android:layout_alignParentBottom="true"
android:paddingStart="15dp"
android:gravity="center"
android:orientation="vertical"
android:background="@drawable/ic_bott_left">
<ImageView
android:layout_
android:layout_
android:src="@drawable/ic_bug"/>
<TextView
android:layout_
android:layout_
android:textColor="@android:color/white"
android:layout_marginTop="10dp"
android:layout_marginBottom="32dp"
android:textSize="18sp"
android:text="Bugs"
android:textStyle="bold"/>
</LinearLayout>
<!--Bottm_Right_Btn-->
<LinearLayout
android:onClick="OnClickFitness"
android:layout_
android:layout_
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:paddingEnd="15dp"
android:gravity="center"
android:orientation="vertical"
android:background="@drawable/ic_bott_right">
<ImageView
android:layout_
android:layout_
android:src="@drawable/ic_fitness"/>
<TextView
android:layout_
android:layout_
android:textColor="@android:color/white"
android:layout_marginTop="10dp"
android:layout_marginBottom="32dp"
android:textSize="18sp"
android:text="Fitness"
android:textStyle="bold"/>
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
& 然后为中的按钮创建 onClick 方法 MainActivity.java
package com.softwindevs.circularpiebuttons;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
public void OnClickAndroid(View view)
Toast.makeText(this, "Android Button Clicked", Toast.LENGTH_SHORT).show();
public void OnClickRide(View view)
Toast.makeText(this, "Ride Button Clicked", Toast.LENGTH_SHORT).show();
public void OnClickBugs(View view)
Toast.makeText(this, "Bugs Button Clicked", Toast.LENGTH_SHORT).show();
public void OnClickFitness(View view)
Toast.makeText(this, "Fitness Button Clicked", Toast.LENGTH_SHORT).show();
&就是这样,这就是你可以施展这个魔法的方法 演示:
对于所有资产和完整的项目代码 查看 Github 仓库:Click Here
祝你好运:)
【讨论】:
这太完美了,一定要看看你的项目,如果还有问题我会评论!谢谢您,先生,请保持保存! 欢迎您,祝您好运! @LosKayos以上是关于如何在android xml中设计4个可点击的圆形字段的主要内容,如果未能解决你的问题,请参考以下文章
在 android Kotlin 中设计弯曲的容器/CardViews