如何管理水平线性布局方向内的文本视图

时间:2017-01-26 10:27:04

标签: android android-linearlayout textview

enter image description here

代码:

LinearLayout linearLayout2;

final JSONArray answer=jsonObject1.getJSONArray("answer");  
 //Here get Answer from question

    ((ViewGroup) findViewById(R.id.linearLayout2)).removeView(linearLayout2);                   //Here remove preselected radiobuttons
    linearLayout2 = new LinearLayout(TestActivity2.this);                                       //Here create new viewgroup when user click next and previous
    linearLayout2.setOrientation(LinearLayout.HORIZONTAL);
    linearLayout2.setGravity(Gravity.FILL_HORIZONTAL);

    indexAns=new String[answer.length()];

    for(int j=0;j<answer.length();j++)
    {
        final JSONObject jsonObject2 = answer.getJSONObject(j);
        final String answer_ans=jsonObject2.getString("answer_ans");                            //get answer from loop

        answer_id=jsonObject2.getString("answer_id");

        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);

        //Here create radio buttons depending on answer loop
        final TextView textBtn = new TextView(TestActivity2.this);
        textBtn.setId(Integer.parseInt(answer_id));
        textBtn.setLayoutParams(params);
        textBtn.setAllCaps(true);
        textBtn.setTextSize(12);
        textBtn.setPadding(5,5,5,5);
        textBtn.setGravity(Gravity.LEFT | Gravity.CENTER |Gravity.RIGHT);
        textBtn.setTextColor(Color.parseColor("#000000"));

        indexAns[j]=answer_id;

1 个答案:

答案 0 :(得分:0)

您可能希望发布布局xml文件。看起来你是以编程方式操作布局,在大多数情况下是不必要的。您可以通过xml布局文件完成相同的目标。

此外,您可以使用样式进行格式化,从而节省大量时间。

查看以下布局文档 https://developer.android.com/guide/topics/ui/declaring-layout.html

查看TextView文档: https://developer.android.com/reference/android/widget/TextView.html