【问题标题】:How to put the View dynamicaly between two LinearLayout?如何在两个 LinearLayout 之间动态放置 View?
【发布时间】:2012-01-09 09:10:59
【问题描述】:

我已将此类数据放入 LinearView。如下代码:

public void showResult()
{
    //db.open();
    //c2 = db.getAllTitles(table_name);

    List<TextView> textListWord = new ArrayList<TextView>(tempEmployerList.size());
    List<TextView> textListAnswer = new ArrayList<TextView>(tempEmployerList.size());
    List<TextView> imageListAnswer = new ArrayList<TextView>(tempEmployerList.size());

    //for(int i = 0; i < c.getCount(); i++)

    //int limit = tempEmployerList.size(); 

    for(int i = 0; i<=tempEmployerList.size()-1; i++)
    {    
        RelativeLayout innerLayout = new RelativeLayout(this);
        innerLayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
        innerLayout.setBackgroundColor(0x00000000);

        // set the Multiple TextView
        TextView qWordTV = new TextView(getApplicationContext());
        TextView aWordTV = new TextView(getApplicationContext());
        TextView aImageView = new TextView(getApplicationContext());

        qWordTV.setText("\n"+"1");
        qWordTV.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
        qWordTV.setTextColor(0xFFFF0000);
        qWordTV.setPadding(3, 0, 0, 0);

        aWordTV.setText("\n"+"2");
        aWordTV.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
        aWordTV.setGravity(Gravity.CENTER);
        aWordTV.setTextColor(0xFFFF0000);    
        aWordTV.setGravity(Gravity.CENTER);
        aWordTV.setPadding(40, 0, 0, 0);
        aWordTV.setTextColor(0xFFFF0000);


        aImageView.setText("\n"+"3");
        aImageView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
        aImageView.setGravity(Gravity.RIGHT);
        aImageView.setTextColor(0xFF000000);
        aImageView.setPadding(0, 0, 9, 0);


        /**** Any other text view setup code ****/    

        innerLayout.addView(qWordTV);
        innerLayout.addView(aWordTV);
        innerLayout.addView(aImageView);

        myLinearLayout.addView(innerLayout);

        textListWord.add(qWordTV); 
        textListAnswer.add(aWordTV);
        imageListAnswer.add(aImageView);

    } 
    //while (limit==0);

}

现在我想在这里动态创建简单视图。 . . 比如:

<View android:layout_height="2dip" android:background="#FF000000" />

在 xml 中。

现在如何动态地制作这样的视图? 请帮助我。 谢谢。

【问题讨论】:

    标签: android xml android-layout android-emulator android-ndk


    【解决方案1】:
    View line = new View(getApplicationContext());
    line.setBackgroundColor(0xFF000000);
    // You may need to convert dip to px here, raw pixels are used in the example:
    LayoutParams layoutParams = new LayoutParams(android.view.ViewGroup.LayoutParams.FILL_PARENT, 2);
    line.setLayoutParams(layoutParams);
    

    【讨论】:

    • 是的,我可以创建直线,但它是垂直的。我希望这条线是水平的。
    • 只需交换 LayoutParams(width, height) 构造函数中的参数。我已经更新了我的答案 - 你现在可以使用它了。
    • 谢谢它的工作,但我还是有一些问题。让我更新一下我的问题。
    • 好的,告诉我是否可以将重力设置为动态创建的线性布局?
    猜你喜欢
    • 2016-10-16
    • 1970-01-01
    • 2021-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-24
    • 2021-01-13
    相关资源
    最近更新 更多