【问题标题】:Button opacity/transparency按钮不透明度/透明度
【发布时间】:2014-01-05 19:06:43
【问题描述】:

main.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

【问题讨论】:

    标签: android xml button transparency opacity


    【解决方案1】:

    查看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"
    />
    

    【讨论】:

      【解决方案2】:

      在代码中,您可以获取按钮的实例并手动设置背景可绘制对象的 alpha。

       Button btn = (Button) findViewById(..);
       btn.getBackground().setAlpha( a );  // where a : 0..255 : 0=transparent, 255=fully opaque
      

      【讨论】:

      【解决方案3】:

      您可能无法通过代码更改其不透明度,因为它们是 9 个补丁。您需要创建自己的 9patch 并将其设置为按钮作为背景。如果你想要纯色,当然你也可以使用类似的东西:

      android:background="#80363636"
      

      #AARRGGBB,前两位数保持多低才能获得不透明度

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-09-20
        • 2018-06-30
        • 1970-01-01
        • 2013-06-05
        • 1970-01-01
        • 1970-01-01
        • 2012-03-05
        • 2020-07-12
        相关资源
        最近更新 更多