【发布时间】:2017-05-25 09:06:59
【问题描述】:
我有两个按钮,我想将它们的形状更改为半圆形。然后将它们并排放置,形成一个完整的圆圈。已附上一张图片以显示我希望按钮的外观。任何帮助将不胜感激。谢谢。
【问题讨论】:
-
您应该为每个按钮使用一张图片。这要容易得多。
-
您是否也想在按钮点击时添加任何视觉反馈?
标签: android xml android-layout android-button
我有两个按钮,我想将它们的形状更改为半圆形。然后将它们并排放置,形成一个完整的圆圈。已附上一张图片以显示我希望按钮的外观。任何帮助将不胜感激。谢谢。
【问题讨论】:
标签: android xml android-layout android-button
您必须创建一个可绘制的 xml 文件。
left_corner.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="1dp"
android:bottomRightRadius="0dp" android:bottomLeftRadius="25dp"
android:topLeftRadius="25dp" android:topRightRadius="0dp"/> // here you have to put dimen as per reqiurement
<solid android:color="@color/green" />
</shape>
right_corner.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="1dp"
android:bottomRightRadius="25dp" android:bottomLeftRadius="0dp"
android:topLeftRadius="0dp" android:topRightRadius="25dp"/> // here you have to put dimen as per reqiurement
<solid android:color="@color/green" />
</shape>
layout.xml
<Linearlayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button android:layout_width="@dimen/_150sdp"
android:layout_height="@dimen/_150sdp"
android:background="@draable/left_corner"/>
<Button android:layout_width="@dimen/_150sdp"
android:layout_height="@dimen/_150sdp"
android:background="@draable/right_corner"/>
</Linearlayout>
这对我有用..
【讨论】:
试试这个方法对我有用
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="center_horizontal"
android:layout_marginTop="15dp" >
<Button
android:layout_width="30dp"
android:layout_height="30dp"
android:background="@drawable/btns"
android:text="A"
android:textColor="@android:color/white"
/>
<View
android:layout_width="1dp"
android:layout_height="2dp"
android:background="#f0f0f0"
/>
<Button
android:layout_width="30dp"
android:layout_height="30dp"
android:background="@drawable/bbb"
android:text="B"
android:textColor="@android:color/white"
/>
</LinearLayout>
</LinearLayout>
btns.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<size android:height="30dp"
android:width="30dp"/>
<solid android:color="#000000"/>
<corners android:topLeftRadius="15dp"
android:bottomLeftRadius="15dp"/>
</shape>
bbb.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<size android:height="30dp"
android:width="30dp"/>
<solid android:color="#000000"/>
<corners android:topRightRadius="15dp"
android:bottomRightRadius="15dp"/>
</shape>
输出
【讨论】:
您好 user23423534, 将 xml 形状作为背景应用到按钮并将按钮并排放置将帮助您解决问题。
【讨论】:
#.首先,为左右half-circle创建两个自定义shapedrawable。
left_half_circle.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners
android:bottomLeftRadius="1000dp"
android:topLeftRadius="1000dp"
android:bottomRightRadius="0dp"
android:topRightRadius="0dp" />
<solid android:color="@android:color/holo_red_light" />
</shape>
right_half_circle.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners
android:bottomLeftRadius="0dp"
android:topLeftRadius="0dp"
android:bottomRightRadius="1000dp"
android:topRightRadius="1000dp" />
<solid android:color="@android:color/holo_green_light" />
</shape>
使用:
1. 创建一个水平的LinearLayout 并在其中添加两个Button。使用属性layout_weight 到Buttons 以获得相等的宽度。
2.将left_half_circle设置为button_left的背景,将right_half_circle设置为button_right的背景。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="200dp"
android:layout_height="200dp"
android:orientation="horizontal"
android:weightSum="2"
android:layout_gravity="center">
<Button
android:id="@+id/button_left"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="RED"
android:textColor="@android:color/white"
android:background="@drawable/left_half_circle" />
<Button
android:id="@+id/button_right"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="GREEN"
android:textColor="@android:color/white"
android:background="@drawable/right_half_circle" />
</LinearLayout>
输出:
希望对你有帮助~
【讨论】:
请检查以下代码。
布局文件:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="50dp"
android:layout_height="100dp"
android:background="@drawable/shape"/>
<Button
android:layout_width="50dp"
android:layout_height="100dp"
android:background="@drawable/shape1"/>
</LinearLayout>
形状可绘制
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#fff"/>
<corners android:bottomLeftRadius="50dp"
android:topLeftRadius="50dp"/>
</shape>
shape1 可绘制
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#fff"/>
<corners android:bottomRightRadius="50dp"
android:topRightRadius="50dp"/>
</shape>
【讨论】: