【问题标题】:Android Dev: Can't generate multiple layouts dynamically from an XML templateAndroid Dev:无法从 XML 模板动态生成多个布局
【发布时间】:2012-03-01 21:42:28
【问题描述】:

制作一个简单的程序,它会生成一个多选表单。我有一个 sing_select.xml 作为提出每个问题的模板。然后,在代码中,我想用一堆自定义的模板填充我的 main.xml。虽然它对第一个问题很有效,但不会显示任何后续问题。不知道我做错了什么。我知道没有重叠,因为我手动隐藏了第一个问题。

Java 文件

public class FormFillerActivity extends Activity
{
    private LinearLayout    mQuestionList;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        //Must come before setContentView or program crashes
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);

        //Must set before accessing layout elements or program crashes
        setContentView(R.layout.main);

        mQuestionList = (LinearLayout) findViewById(R.id.Body_Layout);

        initForm();
    }

    private void initForm()
    {
        int count = 1;

        ArrayList<String> answers = new ArrayList<String>();
        answers.add("Single");
        answers.add("Married");
        answers.add("Separated");
        answers.add("Divorced");
        mQuestionList.addView(addSingSelectQuestion(count++, "What is your marital status?", answers));

        answers.clear();
        answers.add("Male");
        answers.add("Female");
        mQuestionList.addView(addSingSelectQuestion(count++, "What is your gender?", answers));

    }

    private View addSingSelectQuestion(int count, String question, ArrayList<String> answers)
    {
        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View container = inflater.inflate(R.layout.sing_select, null);

        ((TextView) container.findViewById(R.id.Sing_Select_Num)).setText(count + ") ");
        ((TextView) container.findViewById(R.id.Sing_Select_Text)).setText(question);

        RadioGroup rg = (RadioGroup) container.findViewById(R.id.Sing_Select_Answer);

        //Generate radio group answers
        Iterator<String> it = answers.iterator();
        while (it.hasNext())
        {
            RadioButton rb = new RadioButton(rg.getContext());
            RadioGroup.LayoutParams params = new RadioGroup.LayoutParams(RadioGroup.LayoutParams.WRAP_CONTENT,
                    RadioGroup.LayoutParams.WRAP_CONTENT);
            String ans = it.next();

            rb.setId(answers.indexOf(ans));
            rb.setLayoutParams(params);
            rb.setText(ans);
            rb.setTextColor(getResources().getColor(R.color.black));
            rb.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimensionPixelSize(R.dimen.txt_normal));
            rg.addView(rb);
        }

        return container;
    }
}

main.xml(去除不相关的 UI 元素)

<?xml version="1.0" encoding="utf-8"?>
...
    <ScrollView
        android:id="@+id/Body_Scroll"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/Footer"
        android:layout_below="@id/Title"
        android:scrollbars="vertical" >

        <LinearLayout
            android:id="@+id/Body_Layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="@dimen/marg_normal"
            android:padding="@dimen/pad_large" >
        </LinearLayout>
    </ScrollView>
...

sing_select.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/Sing_Select_Layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/white"
    android:padding="8dp" >

    <TextView
        android:id="@+id/Sing_Select_Num"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="#) "
        android:textColor="@color/black"
        android:textSize="@dimen/txt_normal" />

    <TextView
        android:id="@+id/Sing_Select_Text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/Sing_Select_Num"
        android:layout_toRightOf="@+id/Sing_Select_Num"
        android:text="The Question?"
        android:textColor="@color/black"
        android:textSize="@dimen/txt_normal" />

    <RadioGroup
        android:id="@+id/Sing_Select_Answer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/Sing_Select_Text"
        android:layout_below="@+id/Sing_Select_Text"
        android:layout_toLeftOf="@+id/Sing_Select_Trans_Button" >
    </RadioGroup>

    <Button
        android:id="@+id/Sing_Select_Trans_Button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:background="@drawable/btn_big"
        android:padding="8dp"
        android:text="Accept"
        android:textSize="@dimen/txt_button" />

</RelativeLayout>

【问题讨论】:

    标签: java android xml android-layout dynamic


    【解决方案1】:

    尝试将 *Body_Layout* LinearLayout 的方向设置为 vertical。我认为您将第一行之后的下一行推出屏幕(膨胀视图的宽度设置为填充父级)。

    【讨论】:

    • 伙计,我真是个白痴。那解决了它。当我错过这样的事情时讨厌它。
    • @JaySoyer 如果我的回答解决了您的问题,请将其标记为正确。
    • 对不起,伙计。在这里发帖的新手。不知道我能做到这一点。现在应该标记。
    猜你喜欢
    • 1970-01-01
    • 2015-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多