创建包含未知数量的布局子项的视图

时间:2013-09-25 08:27:36

标签: android android-layout

我想创建一个包含未知其他视图作为子项的ViewGroup(如LinearLayout):

<Wizard id=... width=... height=...>
    <WizardStep id=... nextButtonOnClick=... preButtonOnClick=... >
        <LinearLayout ...>
           ...
        </LinearLayout>
    </WIzardStep>

    <WizardStep id=... nextButtonOnClick=... preButtonOnClick=... >
        <RelativeLayout ...>
            ...
        </RelativeLayout>
    </WIzardStep>

    <WizardStep id=... nextButtonOnClick=... preButtonOnClick=... >
        ...
    </WIzardStep>
</Wizard>

这里的Wizard和WizardStep都是ViewGroup。我不知道从哪里开始。扩展ViewGroup和所需的功能是我需要做的吗?我将感谢任何帮助,文档,博客等。

2 个答案:

答案 0 :(得分:0)

这可以通过ViewFlipper来实现。如果你想以编程方式进行,这将是一个开始。 (注意:我没有测试这段代码,只是把它写在了我的头顶)

xml layout flip.xml

<?xml version="1.0" encoding="utf-8"?>
<ViewFlipper xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/flippy"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
</ViewFlipper>

活动看起来像这样

public class MainActivity extends Activity{
    ViewFlipper flip;
    protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
    setContentView(R.layout.flip);
    flip = (ViewFlipper)findViewById(R.id.flip);

}
private void addViews(){
    //This is where you would determine which views to add
    flip.add(****** your view 1 *****)
    flip.add(****** your view 2 *****)
}

每当您解析数据以动态创建数据时,您都会添加视图。 然后,您可以使用以下方式导航视图:

flip.showNext()
flip.showPrevious()

你可以用它做更复杂的事情,但这应该给你基础。

答案 1 :(得分:0)

我的解决方案:

WizardView类:

public class WizardView extends RelativeLayout {
Context context;

private final String KEY_LAST_STEP_PASSED = "KEY_LAST_STEP_PASSED";
private final String KEY_WIZARD_FINISHED = "KEY_WIZARD_FINISHED";

private int currentStep;
private boolean isWizardFinished;

private SharedPreferences preferences;

public WizardView(Context context) {
    super(context);
    this.context = context;
}

public WizardView(Context context, AttributeSet attrs) {
    super(context, attrs);
    this.context = context;
}

public WizardView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    this.context = context;
}

public void startWizard() {
    preferences = context.getSharedPreferences("wizard_pref", Context.MODE_PRIVATE);

    isWizardFinished = preferences.getBoolean(KEY_WIZARD_FINISHED, false);
    if (!isWizardFinished) {
        currentStep = preferences.getInt(KEY_LAST_STEP_PASSED, 0);
        // Make all Steps (except current step) GONE
        int count = getChildCount();
        for (int i = 0; i < count; i++) {
            final View child = getChildAt(i);
            if (currentStep == i) {
                child.setVisibility(View.VISIBLE);
            } else {
                child.setVisibility(View.GONE);
            }
        }
    }
}

public void nextStep() {
    int count = getChildCount();
    getChildAt(currentStep).setVisibility(View.GONE);

    if (currentStep < (count - 1)) {
        currentStep++;
        preferences.edit().putInt(KEY_LAST_STEP_PASSED, currentStep).commit();
        getChildAt(currentStep).setVisibility(View.VISIBLE);
    } else {
        finishWizard();
    }
}

public void previousStep() {
    if (currentStep > 0) {
        getChildAt(currentStep).setVisibility(View.GONE);
        currentStep--;
        preferences.edit().putInt(KEY_LAST_STEP_PASSED, currentStep).commit();
        getChildAt(currentStep).setVisibility(View.VISIBLE);
    }
}

public void finishWizard() {
    isWizardFinished = true;
    preferences.edit().putBoolean(KEY_WIZARD_FINISHED, true).commit();
    setVisibility(View.GONE);
}  
}

WizardStep类:

public class WizardStepView extends RelativeLayout {

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

public WizardStepView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public WizardStepView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

}

带向导的示例布局文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<com.example.wizardtest.WizardView 
    android:id="@+id/wizard"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <com.example.wizardtest.WizardStepView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="gone" >

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:onClick="wizardNext"
            android:text="Next(2)" />
    </com.example.wizardtest.WizardStepView>

    <com.example.wizardtest.WizardStepView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="gone" >

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:onClick="wizardNext"
            android:text="Next(3)" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:onClick="wizardPrevious"
            android:text="Previous(1)" />
    </com.example.wizardtest.WizardStepView>

    <com.example.wizardtest.WizardStepView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="gone" >

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:onClick="wizardPrevious"
            android:text="Previous(2)" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:onClick="wizardNext"
            android:text="Finish" />
    </com.example.wizardtest.WizardStepView>
</com.example.wizardtest.WizardView>

</RelativeLayout>

的活动:

WizardView wizardView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    wizardView = (WizardView) findViewById(R.id.wizard);
    wizardView.startWizard();
}