美好的一天,
我想创建一个新对象,我可以添加到我的Android应用程序View,
我只想根据网络是否连接显示红色或绿色圆圈。我想要一个类似于单选按钮的外观,只是颜色必须改变。
最好只使用图片吗?或者我应该创建一个新的视图对象/组件?如果是这样,我该怎么做呢?
答案 0 :(得分:0)
一个简单的选择: 您可以使用PNG图像。您需要两种颜色的图像才能使用。如果您愿意,可以从PNG中创建9Patch。 9Patch为您提供可以拉伸以适合的图像。
然后在XML文件中使用StateList drawable。 Statelist drawable使您能够根据状态更改图像
这是drawable文件夹中的button_statelist.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="@drawable/redbutton" />
<item android:drawable="@drawable/bluebutton" />
</selector>
您可以使用其他状态而不是state_pressed
在布局文件夹中,将按钮定义为:
<Button
android:id="@+id/button1"
android:background="@drawable/button_statelist"
/>
答案 1 :(得分:0)
Yout可以将样式应用于button / imagebutton / imageview,在其中定义背景的外观和边框半径以获得圆形。将其另存为drawable文件夹中的your_style.xml。代码看起来像这样:
<shape>
<solid
android:color="#f00" />
<stroke
android:width="1dp"
android:color="#d00" />
<corners
android:radius="40dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
使用android:background="@drawable/your_style"
设置形状和背景颜色。如果您想要一个额外的图像(用于获取该单选按钮),您只需添加android:drawableLeft="@drawable/your_overlay_image
。