2010-12-15 4 views
2

저는 Android 개발을 처음 사용하고 있습니다. 힘든 시간을 보내고 있습니다. TextView, EditText 및 SkillDiceButton (Button 클래스의 확장)의 합성보기 (SkillDiceGroup)를 만들고 싶습니다. 나는 순수 코드로 내 SkillDiceGroup를 선언 할 때 작업, 내 XML 레이아웃이 퍼팅이 있습니다Android XML 종합보기

<com.jeremybush.d20.SkillDiceGroup android:id="@+id/skillDiceTest" 
    android:title="Foobar!" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 
    </com.jeremybush.d20.SkillDiceGroup> 

그리고이 코드가 있습니다

public class SkillDiceGroup extends LinearLayout 
{ 
// The View components 
private TextView mTitle; 
private EditText mSkill; 
private SkillDiceButton mDice; 

public SkillDiceGroup(Context context, AttributeSet attrs) 
{ 
    super(context); 

    this.setOrientation(HORIZONTAL); 

    mTitle = new TextView(context); 
     mTitle.setText("foobar"); 
    addView(mTitle, new LinearLayout.LayoutParams(
    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT 
)); 

    mSkill = new EditText(context); 
    addView(mSkill, new LinearLayout.LayoutParams(
    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT 
)); 

    mDice = new SkillDiceButton(context, attrs); 
    mDice.setText("d20"); 
    addView(mDice, new LinearLayout.LayoutParams(
    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT 
)); 
} 

private class SkillDiceButton extends DiceButton 
{ 
    public SkillDiceButton(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    } 

    public void onClick(View view) 
    { 
    modifier = Integer.parseInt(mSkill.getText().toString()); 

    super.onClick(view); 
    } 
} 
} 

이 내가 그것을 할 방법을 작동,하지만 나는 것 3 개의 항목을 xml보기에서 자체적으로 선언하고 싶습니다. 어떻게해야합니까?

답변

1

당신은 예를 들어, 안드로이드 XML 레이아웃에 관한 제공하는 광범위한 documention을 살펴해야합니다 declaring layout, common layout objects, 각 레이아웃 유형의 자세한 예를 가지고 hello views을. 3 개 항목과 함께있는 LinearLayout 튜토리얼, 레이아웃에서 직접 찍은

:
<LinearLayout 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_weight="1"> 
    <TextView 
     android:text="row one" 
     android:textSize="15pt" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1"/> 
    <TextView 
     android:text="row two" 
     android:textSize="15pt" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1"/> 
    <TextView 
     android:text="row three" 
     android:textSize="15pt" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1"/> 
    <TextView 
     android:text="row four" 
     android:textSize="15pt" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1"/> 
    </LinearLayout> 

은 그냥 글고과 SkillDiceButton와 두 번째 두 개의 텍스트 뷰를 교체합니다.

+0

설명서를 읽었지 만이 예제가 완전히 동일하지는 않습니다. 내 코드의 끝에서 mSkill 변수를 참조하는 수정 자 할당이 있습니다. 내가 필요한 논리 때문에, 나는 당신의 모범이 직접적인 번역이라고 생각지 않는다. 나는 (비록 아마) 잘못된 수 있습니다. – zombor

+0

모든 로직은 뷰 클래스가 아닌 Activity 클래스에 있어야합니다. 레이아웃에서 텍스트를 보거나 클릭 리스너를 추가하려는 경우 레이아웃 요소를 ID로 액세스 할 수 있습니다. 메모장 튜토리얼 (http://developer.android.com/guide/tutorials/notepad/index.html)과 같은 완벽한 예제 중 하나를 통해 모든 것이 함께 어울리는 지 확인하는 데 도움이 될 수 있습니다. –

+0

사용자 정의 뷰에 고유 한 추가 속성이 필요한 경우 올바른 xmlns를 추가 할 수있는 올바른 방법입니다. 이 프로젝트의 패널 위젯에는 자신의 속성을 추가하는 방법의 예가 있습니다. http://code.google.com/p/android-misc-widgets/ findViewById를 사용하여 활동의 사용자 정의보기에 대한 참조를 얻고 수동으로 추가 속성을 설정할 수도 있습니다. 두 가지 해결책 모두 잘못되었습니다. – schwiz

0

당신이 당신의 자신의보기/위젯이 '가죽'내부 레이아웃 구조를 만들 wan't, 다양한 장소에서 재사용 할 수 있다면 - 당신이 문서에 대한 creating custom components

또한 태그에 대해 읽어을 읽어야하고, LayoutInflater

그러나 SkillDiceGroup을 한 번만 사용하려는 경우 - Mayra가 제안한대로 레이아웃을 만듭니다.

+0

다른 의견에서 말했듯이, 나는이 것을 15-20 번 재사용 할 것입니다. – zombor