我有一个按钮,如下所示:
<Button
android:text="Submit"
android:id="@+id/Button01"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button>
在我的onCreate()
事件中,我正在调用Button01:
setContentView(R.layout.main);
View Button01 = this.findViewById(R.id.Button01);
Button01.setOnClickListener(this);
应用程序中有一个背景,我想在此提交按钮上设置不透明度。如何为此视图设置不透明度?这是我可以在java端设置的东西,还是我可以在main.xml文件中设置?
在java方面,我尝试了Button01.mutate().SetAlpha(100)
,但它给了我一个错误。
答案 0 :(得分:551)
我对其他人的很多更复杂的答案感到惊讶。
您可以非常简单地在xml中按钮(或任何其他视图)的颜色定义中定义alpha:
android:color="#66FF0000" // Partially transparent red
在上面的例子中,颜色是部分透明的红色。
定义视图的颜色时,格式可以是#RRGGBB
或#AARRGGBB
,其中AA
是十六进制alpha值。 FF
完全不透明,00
完全透明。
如果您需要动态更改代码中的不透明度,请使用
myButton.getBackground().setAlpha(128); // 50% transparent
INT的范围从0
(完全透明)到255
(完全不透明)。
答案 1 :(得分:209)
我猜你可能已经找到了答案,但如果没有(对于其他开发人员),你可以这样做:
btnMybutton.getBackground().setAlpha(45);
这里我将不透明度设置为45.您基本上可以将其设置为 0 (完全透明)到 255 (完全不透明)之间的任何内容
答案 2 :(得分:67)
我建议您在colors.xml文件中创建自定义ARGB color,例如:
<resources>
<color name="translucent_black">#80000000</color>
</resources>
然后将按钮背景设置为该颜色:
android:background="@android:color/translucent_black"
如果你想要使用按钮的形状,你可以做的另一件事是创建一个Shape drawable resource,你可以在其中设置按钮应该是什么样的属性:
file:res / drawable / rounded_corner_box.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#80000000"
android:endColor="#80FFFFFF"
android:angle="45"/>
<padding android:left="7dp"
android:top="7dp"
android:right="7dp"
android:bottom="7dp" />
<corners android:radius="8dp" />
</shape>
然后将其用作按钮背景:
android:background="@drawable/rounded_corner_box"
答案 3 :(得分:60)
我在与TextView遇到类似问题的同时找到了您的问题。通过扩展TextView并覆盖onSetAlpha
,我能够解决它。也许你可以尝试用你的按钮做类似的事情:
import android.content.Context;
import android.util.AttributeSet;
import android.widget.TextView;
public class AlphaTextView extends TextView {
public AlphaTextView(Context context) {
super(context);
}
public AlphaTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public AlphaTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public boolean onSetAlpha(int alpha) {
setTextColor(getTextColors().withAlpha(alpha));
setHintTextColor(getHintTextColors().withAlpha(alpha));
setLinkTextColor(getLinkTextColors().withAlpha(alpha));
return true;
}
}
答案 4 :(得分:46)
根据android docs视图,alpha是介于0和1之间的值。所以要设置它使用类似这样的东西:
View v;
v.setAlpha(.5f);
答案 5 :(得分:39)
从上面来看更容易。 默认的alpha属性用于按钮
android:alpha="0.5"
范围介于0表示完全透明,1表示完全不透明。
答案 6 :(得分:19)
android:background="@android:color/transparent"
以上是我所知道的...... 我认为创建自定义按钮类是最好的主意
API等级11
最近我遇到了这个android:alpha xml属性,它取0到1之间的值。相应的方法是setAlpha(float)。
答案 7 :(得分:14)
虽然btnMybutton.getBackground().setAlpha(45);
很不错,但它只是将alpha应用于背景而不是整个视图。
如果您想要应用alpha来查看使用btnMybutton.setAlpha(0.30f);
。这会将不透明度应用于View。它接受0到1之间的值。
设置视图的不透明度。这是一个从0到1的值,其中0 表示视图完全透明,1表示视图 完全不透明。如果此视图覆盖onSetAlpha(int)以返回 是的,那么这个视图负责应用不透明度本身。 否则,调用此方法等同于调用 setLayerType(int,android.graphics.Paint)并设置硬件 层。注意,将alpha设置为半透明值(0
答案 8 :(得分:8)
我使用ICS / JB遇到了这个问题,因为Holo主题的默认按钮包含略微透明的图像。对于背景,这尤其明显。
Gingerbread vs. ICS +:
复制每个分辨率的所有可绘制状态和图像并使透明图像变得坚固是一种痛苦,所以我选择了更脏的解决方案:将按钮包裹在具有白色背景的支架中。这是一个粗略的XML drawable(ButtonHolder),它正是这样做的:
您的XML文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/Content">
<RelativeLayout style="@style/ButtonHolder">
<Button android:id="@+id/myButton"
style="@style/Button"
android:text="@string/proceed"/>
</RelativeLayout>
</LinearLayout>
<强> ButtonHolder.xml 强>
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="@color/white"/>
</shape>
</item>
</layer-list>
<强> styles.xml 强>
.
.
.
<style name="ButtonHolder">
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:background">@drawable/buttonholder</item>
</style>
<style name="Button" parent="@android:style/Widget.Button">
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:textStyle">bold</item>
</style>
.
.
.
然而,这会产生白色边框,因为Holo按钮图像包含边距以说明按下的空间:
所以解决方案是给白色背景一个边距(4dp适合我)和圆角(2dp)以完全隐藏白色但使按钮固定:
<强> ButtonHolder.xml 强>
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="@android:color/transparent"/>
</shape>
</item>
<item android:top="4dp" android:bottom="4dp" android:left="4dp" android:right="4dp">
<shape android:shape="rectangle">
<solid android:color="@color/white"/>
<corners android:radius="2dp" />
</shape>
</item>
</layer-list>
最终结果如下:
你应该将这种风格定位为v14 +,并为Gingerbread / Honeycomb调整或排除它,因为它们的原始按钮图像尺寸与ICS和JB不同(例如,姜饼按钮背后的这种确切风格会导致在按钮)。
答案 9 :(得分:6)
对于视图,您可以通过以下方式设置不透明度。
$('.submit').click(function () {
var myData = {
MyInformation: $('#name').val(),
File: $('#resources').val(),
}
$.ajax({
url: '/Controller/Action',
type: 'POST',
data: myData,
processData: false
}).done(function () {
}).fail(function () {
});
});
对于大于11的API版本,不推荐使用属性view_name.setAlpha(float_value);
。此后,使用view.setAlpha(int)
之类的属性。
答案 10 :(得分:5)
对于API&lt; 11为textView颜色我做了以下:
int textViewColor = textView.getTextColors().getDefaultColor();
textView.setTextColor(Color.argb(128, Color.red(textViewColor), Color.green(textViewColor), Color.blue(textViewColor))); //50% transparent
有点麻烦,但嘿,它有效: - )
答案 11 :(得分:2)
我知道这已有很多答案,但我发现对于按钮来说,创建自己的.xml选择器并将其设置为所述按钮的背景是最简单的。这样,您也可以在按下或启用时更改状态等等。这是我使用的一个快速片段。如果要为任何颜色添加透明度,请添加前导十六进制值(#XXcccccc)。 (XX ==“颜色的阿尔法”)
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" >
<shape>
<solid
android:color="#70c656" />
<stroke
android:width="1dp"
android:color="#53933f" />
<corners
android:radius="4dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
<item>
<shape>
<gradient
android:startColor="#70c656"
android:endColor="#53933f"
android:angle="270" />
<stroke
android:width="1dp"
android:color="#53933f" />
<corners
android:radius="4dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
</selector>
答案 12 :(得分:1)
如果你使用 Kotlin ,很容易像这样设置 alpha
#!/bin/bash
sqlplus -s $DBUSER/$DBPASS@$DBCONN/$DBSID <<EOF
set colsep '|'
set echo off
@$SCRIPT_PATH/QRY_1.SQL
exit;
EOF
其中值必须是浮点数。