我想更改我的Android LinearLayout背景透明度。在这里,我尝试更改activity_view_full_screen_image.xml中的android:alpha =“ 0.4”值。但是更改了诸如PhotoView透明度之类的布局内容,而不是更改了布局透明度。

这是我的代码:

ViewFullScreenImage.java

import android.graphics.Color;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.LinearLayout;

import com.github.chrisbanes.photoview.PhotoView;
import com.squareup.picasso.Picasso;

import java.io.InputStream;

public class ViewFullScreenImage extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_full_screen_image);
        loadImage();
    }

    public void loadImage(){

        Uri imageUrl = Uri.parse("http://www.ucdmc.ucdavis.edu/hr/images/body/Health-Welness-Businesses.jpg");

        PhotoView photoView = (PhotoView) findViewById(R.id.photo_view);
        Picasso.with(this).load(imageUrl).into(photoView);
    }
}




activity_full_screen_image.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_view_full_screen_image"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:alpha="0.4"
    tools:context="com.example.theace.fullscreen.ViewFullScreenImage">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/my_layout"
        android:orientation="vertical"
        >

        <com.github.chrisbanes.photoview.PhotoView
            android:id="@+id/photo_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>


    </LinearLayout>



</LinearLayout>

最佳答案

android:alpha =“ 0.4”


不要像现在使用android:alpha那样设置整个视图的alpha,而是将其背景设置为半透明颜色:

android:background="#AARRGGBB"


其中AA代表颜色的Alpha(FF是完全不透明的,而00是完全透明的):

android:background="#80FF0000"


会给出50%的透明红色。

文件:https://developer.android.com/guide/topics/resources/more-resources.html#Color

07-24 09:46
查看更多