如何改变背景图像的不透明度?

时间:2016-03-30 12:51:16

标签: android

有什么方法可以改变我在XML文件上设置的背景图像的不透明度?此外,我想证明我将要放入xml文件中的文本,但是,我正在研究同样的问题。 Android工作室还不支持合理的文本。但是,我尝试更改为webview而不是textview但是我将很多错误调用到我的主要活动java,因为我使用case语句来调用布局,而webview的java文件不是片段而是活动。非常感谢您的回复。谢谢

这是XML文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:background="@drawable/minnions1"
android:id="@+id/minnions1"
>
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Safe and Responsible usage of Computer,Internet and Email"
    android:textColor="@android:color/black"
    android:textSize="20sp"
    android:id="@+id/title1"
    android:layout_marginTop="125dp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:gravity="center"
    />
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:gravity ="center"
    android:fillViewport="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="100dp"
    android:id="@+id/scrollView"
    >
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:textColor="#000"
        android:textColorLink="@android:color/black"
        android:textSize="18sp"
        android:text="Using facilities and equipments should always be safe and useful in Information and Communication Technology (ICT)
like computer, email, and internet. It is important for us to study the guides and safe usage of computer, internet
and email in schools.\n \n"
        android:drawablePadding="5dp"
        android:layout_marginLeft="40dp"
        android:layout_marginRight="20dp"
        android:layout_below="@+id/content1"
        android:layout_centerHorizontal="true"
        />
</ScrollView>
</LinearLayout>

此外,这是java文件

package com.android.pet.view;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.doepiccoding.navigationdrawer.R;

public class Lesson1 extends Fragment{

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.onepointone, null);
        return rootView;
    }
}

3 个答案:

答案 0 :(得分:6)

试试这个

View backgroundimage = findViewById(R.id.background);
Drawable background = backgroundimage.getBackground();
background.setAlpha(80);

这里backgroundimage是为了您的理解,您可以替换您想要设置不透明度的背景布局

答案 1 :(得分:2)

您可以使用setAlpha(50)方法以编程方式更改它,在xml中您可以使用android:alpha =&#34; 0.5&#34; 1是最大值,因此0.5表示半透明。

您也可以稍微改变您的设计。 而不是这样做,这样做

<FrameLayout>
<ImageView
    android:background="@drawable/background" 
    android:alpha="0.3" />
<LinearLayout>
//Same as before
</LinearLayout>

将framelayout设为您的父级,并将您的线性布局作为孩子,其余部分将保持不变。给你的imageview宽度匹配父级和它的高度。

答案 2 :(得分:0)

如果您需要更改要用作背景的图像的不透明度,我有解决方案,但您需要务实地更改它

   Drawable mDrawableImage= getResources().getDrawable(R.drawable.mybackgroundimage);

         //   opacity in (alpha) set
   mDrawableImage.setAlpha(50);// 50 %

        // setting the images as background to your Layout eg.Constraint,Relative or else
    mLinearLayout.setBackground(mDrawableImage);