2014-01-05 14 views
1

我需要創建一個佈局,必須一分爲二,這樣的:Android的,我自己畫的,「分」的佈局不工作

------- 
|  | 
| A | 
|  | 
------- 
|  | 
| B | 
|  | 
------- 

其中:
A - 這是這個地方,我可以用手指畫點;
B - 它是一個示例ListView

看起來沒有創建類SampleView的對象,所以在A中繪畫不起作用。
那有什麼問題?
如何修復繪畫?

drawing_activity.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:layout_weight="1" 
android:orientation="vertical" > 



    <View 
     class="com.example.proj.SampleView" 
     android:id="@+id/sampleView1" 
     android:layout_width="wrap_content" 
     android:layout_height="0dp" 
     android:layout_weight=".50" /> 



    <ListView 
     android:id="@+id/listView1" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight=".50" > 
    </ListView> 
</LinearLayout> 

SampleView.java

package com.example.proj; 

import java.util.ArrayList; 
import java.util.List; 

import android.content.Context; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.Paint; 
import android.util.AttributeSet; 
import android.util.Log; 
import android.view.MotionEvent; 
import android.view.View; 
import android.view.View.OnTouchListener; 

public class SampleView extends View implements OnTouchListener { 
    private static final String TAG = "SampleView"; 
    List<Point> points = new ArrayList<Point>(); 
    Paint paint = new Paint(); 

    public SampleView(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
     setFocusable(true); 
     setFocusableInTouchMode(true); 

     this.setOnTouchListener(this); 

     paint.setColor(Color.BLACK); 
     paint.setAntiAlias(true); 

     Log.i(TAG, "Created SampleView!"); 
    } 

    @Override 
    public void onDraw(Canvas canvas) { 
     for (Point point : points) { 
      canvas.drawCircle(point.x, point.y, 5, paint); 
      Log.d(TAG, "Painting: "+point); 
     } 
    } 

    public boolean onTouch(View view, MotionEvent event) { 
     if(event.getAction() != MotionEvent.ACTION_DOWN) 
      return super.onTouchEvent(event); 
     Point point = new Point(); 
     point.x = event.getX(); 
     point.y = event.getY(); 
     points.add(point); 
     invalidate(); 
     Log.d(TAG, "point: " + point); 
     return true; 
    } 

} 


class Point { 
    float x, y; 

    @Override 
    public String toString() { 
     return x + ", " + y; 
    } 
} 

MainActivity.java

package com.example.proj; 

import java.util.ArrayList; 

import android.os.Bundle; 
import android.app.Activity; 
import android.widget.ListView; 

public class MainActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.drawing_activity); 
     final ListView listview = (ListView) findViewById(R.id.listView1); 
     String[] values = new String[] { "Element1", "Element2" }; 

     final ArrayList<String> list = new ArrayList<String>(); 
     for (int i = 0; i < values.length; ++i) { 
      list.add(values[i]); 
     } 
     final StableArrayAdapter adapter = new StableArrayAdapter(this, 
      android.R.layout.simple_list_item_1, list); 
     listview.setAdapter(adapter); 
    } 

} 
+0

您是否有此消息「Created SampleView!」在日誌中? –

+0

不,這很奇怪 –

+0

好吧看到我的答案,希望這可以解決問題.. –

回答

2

喲烏爾XML必須是:

<com.example.proj.SampleView 
     android:id="@+id/sampleView1" 
     android:layout_width="wrap_content" 
     android:layout_height="0dp" 
     android:layout_weight=".50" /> 

OR:觀點與 -capitalized 「V」

<view 
     class="com.example.proj.SampleView" 
     android:id="@+id/sampleView1" 
     android:layout_width="wrap_content" 
     android:layout_height="0dp" 
     android:layout_weight=".50" /> 

兩個代碼視圖,視圖存在,但View將產生一個新的視圖類,而view將產生來自class標籤。

+0

在這兩種情況下,我有: '01-05 20:08:48.497:E/AndroidRuntime(3170):致命例外:main' '01-05 20:08:48.497:E/AndroidRuntime(3170):java.lang.RuntimeException:無法啓動活動ComponentInfo {com.example.proj/com.example.proj.MainActivity}:android.view。 InflateException:二進制XML文件行#10:錯誤膨脹類com.example.proj.SampleView' '01-05 20:08:48.497:E/AndroidRuntime(3170):\t at android.app.ActivityThread.performLaunchActivity(ActivityThread .java:2180)' –

+0

好的,我解決了!我添加了構造函數'public SampleView(Context context)'和'Public SampleView(Context context,AttributeSet attrs)'。我不知道,爲什麼它有效。非常感謝你! :) –

+1

很高興幫助..雖然我不完全確定,但我認爲使用defStyle的構造函數在從XML實例化時被調用 –