android - 按钮不透明度/透明度

标签 android xml button transparency opacity

主.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
     android:background="@drawable/background"
        android:gravity="center"
        android:color="#66FF0000"

    >


    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/noua"
        android:textStyle="bold"
        android:textColor="#808080"
         />


    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/zece"
        android:textStyle="bold"
        android:textColor="#808080" />

    <Button
        android:id="@+id/button3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/unspe"
        android:textStyle="bold"
        android:textColor="#808080" />

    <Button
        android:id="@+id/button4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/doispe"
        android:textStyle="bold"
        android:textColor="#808080"
/>

</LinearLayout>

这是我的 main.xml,我试图使按钮最透明,但我仍然想看到它们,但我不知道要添加什么以及在哪里添加,请用低不透明度更正我的 xml在按钮上。有预谋的谢谢:D

最佳答案

检查 How to Set Opacity (Alpha) for View in Android

您可以设置对象背景的透明度,使用#XX808080,其中 XX 是 alpha/透明度值。

android:background="#80FF0000"

这将是一个半透明的红色背景。

您可以使用 textColor 属性以相同的方式设置按钮文本的透明度:

android:textColor="#80FF0000"

也可以通过动画中的代码实现

AlphaAnimation alphaAnim = new AlphaAnimation(1.0f, 0.5f);
alphaAnim.setDuration (400);
myButton.getBackground().startAnimation(alphaAnim);  // set alpha on background

或直接:

myButton.getBackground().setAlpha(0.5f);   // added in API 11, sets alpha on background

两种方法都将 alpha 设置为 50%

最后,您可以尝试为您的 XML 添加 android:alpha:

<Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/zece"
        android:textStyle="bold"
        android:textColor="#808080" 
android:alpha="0.5"
/>

关于android - 按钮不透明度/透明度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20643999/

相关文章:

xml - SSRS 2005 - XML 网络服务数据集 - 参数未传递给网络服务

button - 如何更改 flutter Material 按钮的字体大小?

swift - xcode 导航栏按钮状态

使用 Robolectric 进行 Android http 测试

android - 如何检查 TextView 字符串是否已被修剪(选取框)?

android - Action 条 Sherlock : Setting and hiding title bar

javascript - 如何使用 jQuery 获取按钮类型按钮的值?

android - 在 fragment 内初始化 map

xml - XSLT 转换为 xml,按键分组

Android spinner dialogMode 样式