2012-04-06 6 views
0

저는이 그래픽/캔버스 전체에 대해 상당히 새롭습니다. 내가하려고하는 것은 캔버스에 몇 개의 버튼 아래에 선을 표시하는 것입니다. 나는 무엇을 잘못 했는가? 여기내 캔버스가 표시되지 않는 이유는 무엇입니까 (Android)?

public class Vectors extends Activity{ 

    VectorsView vectorsView; 
    LinearLayout l; 
    Canvas canvas; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.vectors); 
     vectorsView = new VectorsView(this); 

    } 

    public class VectorsView extends View{ 

     public VectorsView(Context context) { 
      super(context); 
     } 

     @Override 
     protected void onDraw(Canvas canvas) { 
      // TODO Auto-generated method stub 
      super.onDraw(canvas); 
      l = (LinearLayout) findViewById(R.layout.vectors); 


      Paint paint = new Paint(); 
      canvas = new Canvas(); 

      paint.setColor(Color.WHITE); 
      canvas.drawLine(0, 0, 100, 100, paint); 
      vectorsView.draw(canvas); 

      l.addView(vectorsView); 
      setContentView(l); 
     } 
    } 
} 

그리고 요청 vectors.xml한다 : -하지만 당신이 속성을 가진 생성자를 추가해야합니다

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_height="wrap_content" 
    android:layout_width="match_parent"> 
    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:background="@drawable/background" 
     android:orientation="vertical"> 

     <ImageView android:layout_height="wrap_content" 
      android:src="@drawable/vectors" 
      android:layout_width="wrap_content" 
      android:layout_gravity="center" 
      android:paddingTop="5dp" 
      android:paddingBottom="5dp" 
      android:adjustViewBounds="true"> 
     </ImageView> 
     <LinearLayout android:layout_height="wrap_content" 
      android:layout_width="match_parent" 
      android:orientation="horizontal"> 
      <Button android:text="Choose Program" 
       android:id="@+id/bChsProgV" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginBottom="5dp" 
       android:layout_marginLeft="97dp" 
       android:adjustViewBounds="true"> 
      </Button> 
      <ImageButton android:layout_height="wrap_content" 
       android:src="@drawable/help" 
       android:id="@+id/ibHelpV" 
       android:layout_width="wrap_content" 
       android:layout_marginLeft="65dp" 
       android:background="@null" 
       android:layout_marginTop="10dp"> 
      </ImageButton> 
     </LinearLayout> 
     <LinearLayout android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal"> 
      <TextView android:text="Value" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:paddingLeft="5dp" 
       android:paddingRight="5dp" 
       android:textColor="#fffafa"> 
      </TextView> 
      <EditText android:text="" 
       android:id="@+id/etValueV" 
       android:layout_height="wrap_content" 
       android:inputType="numberSigned|numberDecimal" 
       android:layout_width="100dp" > 
      </EditText> 
      <Button android:text="Next" 
       android:id="@+id/bNextV" 
       android:layout_width="65dp" 
       android:layout_height="wrap_content" 
       android:layout_marginLeft="10dp"> 
      </Button> 
      <TextView android:text="" 
       android:id="@+id/tvAnsValV" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content"> 
      </TextView> 
     </LinearLayout> 
     <LinearLayout android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal"> 
      <TextView android:text="Angle" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:paddingLeft="5dp" 
       android:paddingRight="5dp" 
       android:textColor="#fffafa"> 
      </TextView> 
      <EditText android:text="" 
       android:id="@+id/etAngleV" 
       android:layout_height="wrap_content" 
       android:inputType="numberSigned|numberDecimal" 
       android:layout_width="100dp" > 
      </EditText> 
      <Button android:text="Prev" 
       android:id="@+id/bPrevV" 
       android:layout_width="65dp" 
       android:layout_height="wrap_content" 
       android:layout_marginLeft="10dp"> 
      </Button> 
      <TextView android:text="" 
       android:id="@+id/tvAnsAng" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content"> 
      </TextView> 
     </LinearLayout> 
     <LinearLayout android:layout_height="wrap_content" 
      android:layout_width="match_parent" 
      android:orientation="horizontal"> 
      <LinearLayout android:layout_height="wrap_content" 
      android:layout_width="wrap_content" 
      android:orientation="vertical"> 
       <RadioGroup 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" >    
        <RadioButton android:layout_height="wrap_content" 
         android:layout_width="wrap_content" 
         android:checked="true" 
         android:text="Degrees" 
         android:layout_marginLeft="5dp" 
         android:id="@+id/rbDegV"> 
        </RadioButton> 
        <RadioButton android:layout_height="wrap_content" 
         android:layout_width="wrap_content" 
         android:checked="false" 
         android:text="Radians" 
         android:layout_marginLeft="5dp" 
         android:id="@+id/rbRadV"> 
        </RadioButton> 
       </RadioGroup> 
      </LinearLayout> 
      <RadioGroup 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" > 
       <LinearLayout android:layout_height="wrap_content" 
        android:layout_width="wrap_content" 
        android:orientation="horizontal"> 
        <LinearLayout android:layout_height="wrap_content" 
         android:layout_width="wrap_content" 
         android:orientation="vertical"> 
         <RadioButton android:layout_height="wrap_content" 
          android:layout_width="wrap_content" 
          android:checked="false" 
          android:text="Q2" 
          android:layout_marginLeft="90dp" 
          android:id="@+id/rbQ2V"> 
         </RadioButton> 
         <RadioButton android:layout_height="wrap_content" 
           android:layout_width="wrap_content" 
           android:checked="false" 
           android:text="Q3" 
           android:layout_marginLeft="90dp" 
           android:id="@+id/rbQ3V"> 
         </RadioButton> 
        </LinearLayout> 
        <LinearLayout android:layout_height="wrap_content" 
         android:layout_width="wrap_content" 
         android:orientation="vertical"> 
         <RadioButton android:layout_height="wrap_content" 
          android:layout_width="wrap_content" 
          android:checked="true" 
          android:text="Q1" 
          android:layout_marginLeft="5dp" 
          android:id="@+id/rbQ1V"> 
         </RadioButton> 
         <RadioButton android:layout_height="wrap_content" 
           android:layout_width="wrap_content" 
           android:checked="false" 
           android:text="Q4" 
           android:layout_marginLeft="5dp" 
           android:id="@+id/rbQ4V"> 
         </RadioButton> 
        </LinearLayout> 
       </LinearLayout> 
      </RadioGroup>              
     </LinearLayout> 
     <Button android:text="Submit" 
      android:id="@+id/bSubmitV" 
      android:layout_width="75dp" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center"> 
     </Button> 
     <TextView android:text="" 
      android:id="@+id/tvErrorV" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:textColor="#fffafa"> 
     </TextView> 
     <LinearLayout android:id="@+id/llCanvasV" 
      android:layout_height="wrap_content" 
      android:layout_width="match_parent"> 
     </LinearLayout> 
    </LinearLayout> 

</ScrollView> 
+0

당신이 당신의 vectors.xml을 표시 할 수 있습니다 :

이 페이지는 몇 가지 추가 정보를 제공합니다? – ligi

답변

0

중 하나를 당신이 당신의 vectors.xml에서 VectorsView 사용 여기 내 코드입니다

또는

당신이 당신의 vectors.xml에 컨테이너 (예를 들어있는 LinearLayout)를 추가하고 코드를 통해에 vectorsView를 추가 - 다음이 생성자는 충분하다

0

보기 계층은 XML로 정의됩니다.

사용자의 setContentView() 호출은 최상위 XML 요소 인 ScrollView를 뷰 계층의 루트보기로 만듭니다.

코드에서 만든 VectorsView 객체는 해당 뷰 계층 구조에 연결되지 않습니다. 해당 계층 구조에 있지 않으므로 결코 표시되지 않습니다. onDraw() 코드는 결코 호출되지 않습니다.

onDraw() 루틴 내에서 addView() 및 setContentView()를 호출하는 것은 부적절합니다. 일반적인 순서는 onCreate()에서 호출 된 코드로 뷰 계층 구조를 정의한 다음 OS가 전체 계층 구조 전체에서 onDraw() 메서드를 호출하도록 허용하는 것입니다.

onDraw()에서 드로잉 관련 명령을 제외한 모든 것을 제거한 경우 setContentView (R.layout.vectors) 호출을 setContentView (vectorsView)로 바꾸면이를 볼 수 있습니다. 물론 XML로 정의 된 위젯은 보이지 않습니다.

http://developer.android.com/guide/topics/ui/index.html

관련 문제