我有一个ImageButton,背景图像有一些透明度.默认情况下,由于Holo.Light主题,按钮会获得透明像素所在的灰色背景.我尝试通过该setBackgroundColor(Color.TRANSPARENT)
方法将按钮的背景颜色设置为透明.这很好,并且做我需要的东西,除了现在我的按钮在聚焦/按下时不再具有浅蓝色并且看起来相当平坦(没有边框,所以它看起来像图像).
我用google搜索,看到你可以像这里解释的那样分配选择器,但这意味着我必须指定每个按钮状态的图像,我不想这样做.我想继承主题中的焦点/按下的颜色,但是将正常的按钮背景(未按下/聚焦时)覆盖为透明而不是灰色.我怎么能实现那个?请提供一个工作示例,因为我尝试了许多不同的组合但没有成功.
编辑 谢谢大家的帮助.我想出了如何使这项工作无需为每个按钮重新创建具有聚焦和按下状态的相同图像!
这是我的解决方案:
我的按钮定义为:
<ImageButton
android:id="@+id/ImageButton05"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@drawable/button" />
Run Code Online (Sandbox Code Playgroud)
我的后台XML文件(标题为button.xml)定义如下:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<layer-list>
<item android:drawable="@drawable/btn_default_pressed_holo_light"></item>
<item android:drawable="@drawable/network_wifi"></item>
</layer-list>
</item>
<item android:state_focused="true">
<layer-list>
<item android:drawable="@drawable/btn_default_focused_holo_light"></item>
<item android:drawable="@drawable/network_wifi"></item>
</layer-list>
</item>
<item android:state_hovered="true">
<layer-list>
<item android:drawable="@drawable/btn_default_focused_holo_light"></item>
<item android:drawable="@drawable/network_wifi"></item>
</layer-list>
</item>
<item>
<layer-list>
<item android:drawable="@drawable/btn_default_normal_holo_light"></item>
<item android:drawable="@drawable/network_wifi"></item>
</layer-list>
</item>
</selector>
Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个看起来如上图所示的按钮.最初我的想法是创建一个9补丁并将其设置为按钮背景.但是,因为这是一个简单的按钮,我想我们可以在不使用任何图像的情况下以某种方式绘制它.
按钮背景颜色为#0c0c0c边框颜色为#1a1a1a文本颜色为#cccccc
我在SO上发现了一个类似的问题,但它创建了一个渐变 -
Android - 按钮的边框
如何使按钮显示在Android中使用自定义背景图像单击按钮(通过设置下降/某些更改).我不想包含更多图像,并将不同的图像设置为不同的状态,如google hello views示例中所示.谢谢.
我想在图像周围画一个边框.但我无法在ImageView本身对齐边框(就像它主要完成一样),因为我使用ImageMatrix翻译和缩放ImageView内部的图像(ImageView本身是fill_parent /填充整个屏幕).我有想法添加第二个图像(看起来像一个边框)并以与应该有边框的图像相同的方式平移和缩放它,但这样做并不是很方便.有没有人更好地实现这一目标?
我想创建4个正方形,在每个正方形中我想创建一个小按钮.广场必须是可见的,并且必须有边框.
我知道如何创建4个按钮作为正方形但我不知道如何在每个正方形周围创建边框.但我想要与尺寸无关,现在按钮非常大......
我的例子
<?xml version="1.0" encoding="utf-8"?>
<ViewFlipper xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent" android:layout_width="fill_parent">
<RelativeLayout android:id="@+id/magaLoginLayout"
android:layout_height="fill_parent" android:layout_width="fill_parent">
<Button android:text="@+id/Button01" android:id="@+id/Button01"
android:layout_width="160dip" android:layout_height="160dip" android:layout_marginTop="20dip"></Button>
<Button android:text="@+id/Button03" android:layout_below="@+id/Button01" android:id="@+id/Button03"
android:layout_alignLeft="@+id/Button01" android:layout_height="160dip" android:layout_width="160dip"></Button>
<Button android:text="@+id/Button04" android:layout_below="@+id/Button01" android:id="@+id/Button04"
android:layout_toRightOf="@+id/Button03" android:layout_height="160dip" android:layout_width="160dip"></Button>
<Button android:text="@+id/Button02" android:id="@+id/Button02" android:layout_width="wrap_content"
android:layout_toRightOf="@+id/Button01" android:layout_alignTop="@+id/Button01" android:layout_alignParentRight="true" android:layout_height="160dip"></Button>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
我有这样的可绘制(xml)。
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<corners android:radius="10dp"/>
<padding android:left="5dp" android:right="5dp" android:top="5dp" android:bottom="5dp"/>
<solid android:color="#FF00FF00"/>
</shape>
Run Code Online (Sandbox Code Playgroud)
我的按钮在父线性布局内。
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="@drawable/custombordergreen"
android:orientation="vertical">
<Button
android:id="@+id/startClock"
style="?android:attr/buttonStyleSmall"
android:layout_width="204dp"
android:layout_height="66dp"
android:text="@string/start"
android:textSize="22sp"
android:layout_gravity="center_horizontal"
android:background="@drawable/startbutton"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
但是,如果按钮已经是相对布局中的同级(并在“ toLeftOf”中引用),则无法使用该技术。
缺少重新布局以避免“相对”布局的方法,还有另一种方法可以在按钮周围放置边框吗?
嗨,我用按钮覆盖了我的 ImageView,但是当它们彼此靠近时,你无法分辨它是按钮,它只是一个大红色方块。我需要一种快速简便的方法来为我的按钮设置边框/边缘或其他东西,以便您可以看到不同的按钮。