2014-06-17 2 views
0

프로그래밍 방식으로 TextView 및 EditView를 내 사용자 정의 LinearLayout에 추가하고 싶습니다. 하지만 어떻게해야할지 모르겠다. 이 같은 뭔가 (작동하지 않는) :사용자 정의 LinearLayout에서 standart보기를 사용할 수 있습니까

<com.custom.FavoritesViewer 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:visibility="gone" 
      android:id="@+id/favoritesViewer"> 
     <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Hello"/> 
</com.custom.FavoritesViewer> 

내 사용자 정의 레이아웃

public class FavoritesViewer extends LinearLayout { 

    private Bitmap fullImage; 
    private int canvasWidth; 
    private int canvasHeight; 

    private final Paint paint = new Paint(); 

    public FavoritesViewer(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     setWillNotDraw(false); 
     initializeCanvasSize(context); 
    } 

    private void initializeCanvasSize(Context context) { 
     final Pair<Integer, Integer> screenSize = Utils.getScreenSize(context); 
     canvasWidth = screenSize.first; 
     canvasHeight = screenSize.second; 
    } 

    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     setMeasuredDimension(canvasWidth, canvasHeight/3); 
    } 

    @Override 
    protected void onDraw(Canvas cvs) { 
     if (fullImage == null) { 
      fullImage = Bitmap.createBitmap(canvasWidth, canvasHeight/3,  Bitmap.Config.ARGB_8888); 
      final Canvas canvas = new Canvas(fullImage); 
      paint.reset(); 
      paint.setColor(Color.parseColor("#AA000000")); 
      canvas.drawRect(0, 0, canvasWidth, canvasHeight/3, paint); 
     } 
     cvs.drawBitmap(fullImage, 0, 0, null); 
    } 

} 

그래서 내가 캔버스가 (배경 등) 내가 정상에 약간의 standart보기를 추가하고 싶습니다 . onDraw에 추가 할 수 없습니다.

보기를 사용자 정의 레이아웃에 추가하는 방법이 있습니까?

편집 해

나는 버튼을 몇 가지 특별한 UI를 구현해야합니다. 이 구성 요소를 하나의 구성 요소로 묶고 싶습니다. 캔버스에 그 UI를 그리고 어떻게 든 단추를 추가해야합니다 (이미지를 그리거나 단추의 동작을 에뮬레이션하지 않고 간단한 ImageButton을 추가하는 것으로 충분합니다). 그래서 레이아웃을 컨테이너로 선택하고 뷰를 프로그래밍 방식으로 추가해야합니다.

+0

예 가능합니다. –

+0

addView() 메소드가 있다는 것을 알고 있습니까? 어쩌면 일부 문서를 읽을 수 있습니다 ... http://developer.android.com/reference/android/view/ViewGroup.html#addView(android.view.View, int, android.view.ViewGroup.LayoutParams) – ElDuderino

답변

0

super (아마도 super.onDraw())를 호출하는 한 부모 클래스가 예상대로 추가하는 뷰를 그릴 것이라고 생각합니다. 부모 LinearLayout 클래스가 내용을 렌더링하지 못하게하는 onDraw를 재정의하는 것처럼 보입니다 (TextView와 같은).

먼저 onDraw 메서드를 주석 처리하고 LinearLayout이 예상대로 작동하는지 확인하십시오.

또한 맞춤 레이아웃의 목표는 무엇입니까? 목표를 달성하는 더 좋은 방법이있을 수 있습니다.

관련 문제