Android 如何为我的相对布局设置 alpha 值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11327523/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:59:35  来源:igfitidea点击:

How to set alpha value for my Relative layout?

androidlayoutalpha

提问by balaji

Hi I'm trying to set alpha value for my relative layout but, i am getting error how to solve this help me.....

嗨,我正在尝试为我的相对布局设置 alpha 值,但是我遇到错误如何解决这个问题,请帮助我.....

I have three layout in my xml layout 1st layout using for background 2nd layout using for header 3rd layout using for footer. I wish to set alpha value 2 & 3rd layout so i am trying many ways still i have no idea please tell how to set alpha value

我的 xml 布局中有 3 个布局,第一个布局用于背景,第二个布局用于页眉,第三个布局用于页脚。我想设置 alpha 值 2 和 3rd 布局,所以我尝试了很多方法,但我不知道请告诉如何设置 alpha 值

xml code:

xml代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_alignParentLeft="true"
    android:orientation="vertical" 
    android:background="@drawable/blue">

    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="60px"
        android:orientation="horizontal"
        android:layout_alignParentTop="true"
        android:background="@drawable/gradient_black"
        android:id="@+id/ttest">
       <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

        />

        <TextView
            android:id="@+id/header_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginLeft="6dp"
            android:layout_toRightOf="@+id/imageView1"
            android:text="settings"
            android:textColor="@color/white"
            android:textSize="20sp" />

    </RelativeLayout>

    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="55px"

        android:background="@drawable/gradient_black"
        android:gravity="bottom"
        android:orientation="horizontal"
        android:layout_alignParentBottom="true" >
  <ImageView
            android:id="@+id/imageView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="ssss"
        />  

    </RelativeLayout>

</RelativeLayout>

code:

代码:

public class DesignActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        int width = getWindowManager().getDefaultDisplay().getWidth();
        int height = getWindowManager().getDefaultDisplay().getHeight();


        ImageView imgHead = (ImageView)findViewById(R.id.imageView1);

        ImageView imgbottom = (ImageView)findViewById(R.id.imageView2);


        imgbottom.setImageResource(R.drawable.back);
        imgbottom.setLayoutParams(new RelativeLayout.LayoutParams(width/8, height/8));


        imgHead.setImageResource(R.drawable.b);
        imgHead.setLayoutParams(new RelativeLayout.LayoutParams(width/8, height/8));
     //   RelativeLayout relative = (RelativeLayout)findViewById(R.id.ttest);

    }
}

回答by Manikandan

try this

尝试这个

AlphaAnimation alpha = new AlphaAnimation(0.5F, 0.5F);
alpha.setDuration(0); // Make animation instant
alpha.setFillAfter(true); // Tell it to persist after the animation ends
// And then on your layout
yourLayout.startAnimation(alpha);

回答by SMD

RelativeLayout rl; ...
rl.setAlpha(0.5F);

相对布局 rl; ...
rl.setAlpha(0.5F);

<RelativeLayout
    android:id="@+id/rl"
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:background="@drawable/bg"
    android:alpha="0.5">

回答by luke-009

In these cases I generally tend to want to set the colour and alpha at the same time, so I just use rl.setBackgroundColor(0xAACCCCCC);where A is alpha value and C is colour, in hex format.

在这些情况下,我通常倾向于同时设置颜色和 alpha,所以我只使用rl.setBackgroundColor(0xAACCCCCC);其中 A 是 alpha 值而 C 是颜色的十六进制格式。

e.g: rl.setBackgroundColor(0x88000000);for 0.5 transparent black background.

例如:rl.setBackgroundColor(0x88000000);0.5 透明黑色背景。

or in XML: android:background="#88000000"

或在 XML 中: android:background="#88000000"