2016-03-30 139 views
1

有没有什么办法可以改变我在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; 
    } 
} 
+0

我不能完全理解你的第二部分任务..其中是你的webview,它现在可以在代码上传.. – sharan

回答

3

尝试这个

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

这里将backgroundImage是你的理解,你可以更换你的布局背景你想设置不透明度

+0

公共视图onCreateView(LayoutInflater inflater,ViewGroup容器,Bundle savedInstanceState){ 查看rootView = inflater.inflate(R.layout.onepointone,null); 查看backgroundimage = findViewById(R.id.minnions1); Drawable background = backgroundimage.getBackground(); background.setAlpha(80); return rootView; }} 但是有findViewById下红线说无法解析方法findViewById(int)的 – Dreamer

+0

@Dreamer尝试使用rootView.findViewById(R.id.minnions1); – sharan

+0

谢谢,它现在工作得很好 – Dreamer

2

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

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

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

让您的FrameLayout父母,把你的线性布局作为一个孩子,其余部分将保持不变。给你匹配父母的高度的imageview宽度,就是这样。

+0

然而,如果我将在我的xml文件中实现它,它会影响我的textview。我已经尝试过了,整个布局都受到了影响。我想要做的只是背景图像而不是文字浏览 – Dreamer

+0

那么在这种情况下,您必须稍微改变一下设计,使用framelayout并在其中使用imageview,然后更改该图像的alpha值,而不会影响其他 –

+0

我已更新我的答案 –

相关问题