2016-03-30 138 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

我已更新我的答案 –

相關問題