2017-05-15 23 views
0

目前我想我的屏幕上的兩個點之間增加一條線,當我申請的內容視圖出現問題,並去除了以前的內容,並只適用於行。目前,我有一個形象,一個複選框,並在我的活動的.xml文件一個TextView,代碼如下:的setContentView消除了對我的活動的其他組件

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/content" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:foreground="?android:attr/selectableItemBackground"> 

    <ImageView 
     android:id="@+id/mapa" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_gravity="center" 
     android:adjustViewBounds="true" 
     android:scaleType="fitXY" 
     android:src="@drawable/mapau" /> 
    <ImageView 
     android:id="@+id/pointer" 
     android:layout_width="30dp" 
     android:layout_height="30dp" 
     android:src="@drawable/pointer" 
     android:visibility="gone"/> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:id="@+id/tv" 
     android:layout_centerInParent="true" 
     android:layout_gravity="bottom"/> 

    <CheckBox 
     android:id="@+id/checkBox" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:checked="false" 
     android:duplicateParentState="false" 
     android:text="Disponible" 
     android:textSize="20sp" 
     android:onClick="onCheckboxClicked" 
     android:theme="@style/WhiteCheck"/> 
</FrameLayout> 

我使用的是Java類添加上述線路,把它在我裏面主要的代碼我onStartprotectedvoid

drawView = new DrawView(this); 
setContentView(drawView); 

我使用的Java類:

package com.example.vitto.udlc; 

import android.content.Context; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.Paint; 
import android.view.View; 

public class DrawView extends View { 
    Paint paint = new Paint(); 

    public DrawView(Context context) { 
     super(context); 
     paint.setColor(Color.BLACK); 
    } 

    @Override 
    public void onDraw(Canvas canvas) { 
     canvas.drawLine(0, 0, 200, 200, paint); 
     canvas.drawLine(200, 0, 0, 200, paint); 
    } 

} 

我是相當新到Android工作室,這樣我明明看着上方克東西,任何幫助,不勝感激。

+0

的setContentView方法來設置視圖的某些活動是不是意味着該視圖會出現會drawView函數只有 –

+0

的setContentView設置該活動的整個佈局,它並不像追加或覆蓋。 –

回答

1

嗯,我認爲你必須使用:

setContentView(R.layout.yourlayout); //all this code in your activity 
DrawView drawView= new DrawView(this); 
ViewGroup frameLayout = (ViewGroup) findViewById(R.id.content); 

drawView.setLayoutParams(new 
FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, 
FrameLayout.LayoutParams.WRAP_CONTENT)); 
frameLayout.addView(drawView); 

這樣您設置的背景佈置,然後添加在佈局線。

+0

它像一個魅力,非常感謝你。讓我看看,如果我得到這個權利,使用此代碼,我們做一個新的相對佈局,並設置新的佈局,而不是以前的佈局drawView函數? –

+0

@VittoRusso查看答案。我更新了它的framelayout – Curio

0

你可能不是「刪除」舊的組件,您的問題更可能的原因是在畫布上佔據整個屏幕,當你畫它,它掩蓋了它背後的一切。

開放層次觀衆,看看帆布或不覆蓋舊的元素,如果這樣做,你現在知道你的問題的根源。下面是如何使用層次結構查看器的鏈接:https://developer.android.com/studio/profile/hierarchy-viewer.html

當畫布「onDraw」方法被調用時會發生什麼,它會生成畫布,然後在每次調用onDraw方法時繪製它,這會把它帶到前面,所以,除非你以某種方式告訴其他UI元素將被帶到前面,他們將得到掩蓋。

你也可以檢查帆布只是讓畫布透明掩蓋它,試試這個代碼中的onDraw方法:

canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.OVERLAY); 
相關問題