I'm new to Android development, and am havi开发者_开发百科ng a tough time working something out. I want to make a composite view (called SkillDiceGroup) of a TextView, EditText, and SkillDiceButton (which is an extension of the Button class). I have it working when declaring my SkillDiceGroup as pure code, and putting this in my XML layout:
<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>
And I have this code:
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);
}
}
}
This works how I want it, but I would like to declare the three items in the xml view on their own. How can I do this?
You should take a look at the extensive documention Android provides regarding xml layouts, for example: declaring layout, common layout objects, and hello views which has an detailed example of each layout type.
Taken directly from the LinearLayout tutorial, a layout with 3 items:
<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>
Just replace the second two text views with an EditText and a SkillDiceButton.
If you wan't to create your own view/widget that 'hides' internal layout structure, and can be reusable in various places - you should read document about creating custom components
also read about tag, and LayoutInflater
But if want to use SkillDiceGroup only once - just create layout as proposed by Mayra
精彩评论