Android 如何动态添加组件

Android 如何动态添加组件,android,Android,我必须显示从数据库中拍摄的图片网格。根据用户的屏幕大小,我想显示更多或更少的图片 1) 如何根据数据库中的图片数量动态更改显示的图片数量 1a)哪种布局合适 2) 如果一个屏幕上的图片数量超过了这个数字,那么很明显,它必须被滚动。我如何定义页面滚动而不是一点一点滚动,这意味着每次滚动后,下一页将有所有新成员(就像我们在Android中滚动应用程序一样) 目前,我有一个用于主活动的TabHost布局和一个用于网格类型显示活动的LinearLayout 我使用的是API版本10,所以GridLayo

我必须显示从数据库中拍摄的图片网格。根据用户的屏幕大小,我想显示更多或更少的图片

1) 如何根据数据库中的图片数量动态更改显示的图片数量

1a)哪种布局合适

2) 如果一个屏幕上的图片数量超过了这个数字,那么很明显,它必须被滚动。我如何定义页面滚动而不是一点一点滚动,这意味着每次滚动后,下一页将有所有新成员(就像我们在Android中滚动应用程序一样)

目前,我有一个用于主活动的TabHost布局和一个用于网格类型显示活动的LinearLayout

我使用的是API版本10,所以GridLayout不可用

任何建议都会很有帮助。提前感谢。

1)如何根据数据库中的图片数量动态更改显示的图片数量?

就这一部分而言,根据数据库中可用的图片数量使用for循环,您唯一需要的是知道数据库中存在的元素数量,然后使用这些元素,就像我在这里使用的
numberOfElements
一样

 for(int i = 1; i <= numberOfElements ; i++) {

    LinearLayout lHor = new LinearLayout(this);
    lHor.setOrientation(LinearLayout.HORIZONTAL);
    //lHor.setBackgroundColor(Color.rgb(238,    233,    191));

    // Text View
    TextView tv = new TextView(this);
    tv.setText("FAULT "+i);
    tv.setTextSize(20);
    // tv.setGravity(Gravity.CENTER);
    tv.setTextColor(Color.rgb(255,255,255));
    tv.setPadding(12, 12, 12, 12);
    tv.setId(i);
    tv.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1.0f));
    lHor.addView(tv);       // Adding the TextView to the LinearLayout

    //CheckBox
    CheckBox cb = new CheckBox(this);
    cb.setId(i);
    cb.setTag("CheckBox");
    cb.setClickable(false);
    //  cb.setChecked(true);
    cb.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1.0f));
    cb.setGravity(Gravity.CENTER);
    checkBoxes.add(cb);
    lHor.addView(cb);
    l.addView(lHor);

}
=============编辑================

您可以在活动的onCreate()方法中调用此方法

private void setDynamicContentViewOfThisPage() {

// Defining the Scroll View and the LinearLayout
ScrollView sv = new ScrollView(this);
LinearLayout l = new LinearLayout(this);
l.setOrientation(LinearLayout.VERTICAL);
sv.addView(l);

// You will need to collect data from the previous Intent :-)

    TextView introduction = new TextView(this);
    introduction.setText("Set Text Here");
    introduction.setGravity(Gravity.LEFT);
    introduction.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
    l.addView(introduction);


    Button instructionsButton = new Button(this);
    instructionsButton.setTag("Some Button");
    instructionsButton.setId(987654321);    // Random ID set to avoid conflicts :-D
    instructionsButton.setText("Click to read all the instructions");
    instructionsButton.setGravity(Gravity.CENTER);
    instructionsButton.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
    l.addView(instructionsButton);

    instructionsButton.setOnClickListener(this);


                     // Creates a line
                        TableLayout tl1 = new TableLayout(this);
                            View v1 = new View(this);
                            v1.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 1));
                            v1.setBackgroundColor(Color.rgb(51, 51, 51));
                            tl1.addView(v1);
                        l.addView(tl1);

                    // Version 2 (Creating Different Layouts)        
                            for(int i = 1; i <= 3 ; i++) {
                                // Creates a line
                                TableLayout tl2 = new TableLayout(this);
                                    View v2 = new View(this);
                                    v2.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 1));
                                    v2.setBackgroundColor(Color.rgb(51, 51, 51));
                                    tl2.addView(v2);
                                l.addView(tl2);

                                LinearLayout lHor = new LinearLayout(this);
                                lHor.setOrientation(LinearLayout.HORIZONTAL);
                                //lHor.setBackgroundColor(Color.rgb(238,    233,    191));

                                    LinearLayout lVer1 = new LinearLayout(this);
                                    lVer1.setOrientation(LinearLayout.VERTICAL);
                                    lVer1.setGravity(Gravity.RIGHT);
                                    lVer1.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1.0f));

                                        // Text View
                                        TextView tv = new TextView(this);
                                        tv.setText("TV "+i);
                                        tv.setTextSize(20);
//                                          tv.setGravity(Gravity.CENTER);
                                        tv.setTextColor(Color.rgb(255,255,255));
                                        tv.setPadding(12, 12, 12, 12);
                                        tv.setId(i);
//                                          tv.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1.0f));
                                        lVer1.addView(tv);      // Adding the TextView to the LinearLayout

                                        //CheckBox
                                        CheckBox cb = new CheckBox(this);
                                        cb.setClickable(false);
//                                          cb.setChecked(true);
                                        cb.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1.0f));
                                        cb.setGravity(Gravity.CENTER);
                                        lVer1.addView(cb);

                                 lHor.addView(lVer1);

                                    LinearLayout lVer = new LinearLayout(this);
                                    lVer.setOrientation(LinearLayout.VERTICAL);
                                    lVer.setGravity(Gravity.RIGHT);
                                    lVer.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1.0f));

                                        Button showsomeOtherButton = new Button(this);
                                        showsomeOtherButton.setTag("showSomeButton");
                                        showsomeOtherButton.setId(i);
                                        showsomeOtherButton.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1.0f));
                                        showsomeOtherButton.setText("View Image");
//                                          showsomeOtherButton.setEnabled(false);
                                        lVer.addView(showsomeOtherButton);

                                        Button someOtherDataButton = new Button(this);
                                        someOtherDataButton.setId(i);
                                        someOtherDataButton.setTag("someOtherButton");
                                        someOtherDataButton.setText("Do this action " + i);
//                                          someOtherDataButton.setEnabled(false);
                                        someOtherDataButton.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1.0f));
                                        lVer.addView(someOtherDataButton);

                                        showsomeOtherButton.setOnClickListener(this);
                                        someOtherDataButton.setOnClickListener(this);

                                lHor.addView(lVer);
                                l.addView(lHor);

                                // Creates a line
                                TableLayout tl3 = new TableLayout(this);
                                    View v3 = new View(this);
                                    v3.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 1));
                                    v3.setBackgroundColor(Color.rgb(51, 51, 51));
                                    tl3.addView(v3);
                                l.addView(tl3);

                               }

                            // Creates a line
                            TableLayout tl3 = new TableLayout(this);
                                View v3 = new View(this);
                                v3.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 1));
                                v3.setBackgroundColor(Color.rgb(51, 51, 51));
                                tl3.addView(v3);
                            l.addView(tl3);

        Button nextPageButton = new Button(this);
        nextPageButton.setTag("goToNExtPageButton");
        nextPageButton.setId(98765432);
        nextPageButton.setText("Go To Next Page");
        nextPageButton.setGravity(Gravity.CENTER);
        //nextPageButton.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        LinearLayout.LayoutParams layoutParams=new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        layoutParams.gravity=Gravity.CENTER;
        nextPageButton.setLayoutParams(layoutParams);

        l.addView(nextPageButton);

// Set the content View to this
    this.setContentView(sv);    
}
}
private void setDynamicContentViewOfThisPage(){
//定义滚动视图和线性布局
ScrollView sv=新的ScrollView(此);
线性布局l=新的线性布局(本);
l、 设置方向(线性布局。垂直);
sv.addView(l);
//您需要从前面的意图中收集数据:-)
TextView简介=新的TextView(本);
introduction.setText(“在此处设置文本”);
简介.设置重力(重力.左);
简介.setLayoutParams(新的LinearLayout.LayoutParams(LayoutParams.FILL\u父级,LayoutParams.WRAP\u内容));
l、 addView(导言);
按钮说明按钮=新按钮(此按钮);
设置标签(“某些按钮”);
指令按钮.setId(987654321);//为避免冲突而设置的随机ID:-D
instructionsButton.setText(“单击以阅读所有说明”);
说明按钮。设置重力(重心);
说明Button.setLayoutParams(新的LinearLayout.LayoutParams(LayoutParams.FILL\u父项,LayoutParams.WRAP\u内容));
l、 添加视图(说明按钮);
instructionsButton.setOnClickListener(此);
//创建一条线
TableLayout tl1=新的TableLayout(本);
视图v1=新视图(本视图);
v1.setLayoutParams(新的TableRow.LayoutParams(TableRow.LayoutParams.FILL_父项,1));
v1.setBackgroundColor(Color.rgb(51,51,51));
tl1.addView(v1);
l、 addView(tl1);
//版本2(创建不同的布局)
对于(int i=1;i
  • 我使用LinearLayout解决了此问题。我使用以下方法动态添加视图:

    LinearLayout parentLayout = (LinearLayout) findViewById(R.id.parentLayout);
    TextView ChildView = new TextView(getApplicationContext());
    ChildView.setText("I am the child");
    parentLayout.addView(ChildView);
    
    我循环到Sumit建议的可用视图

  • 我不得不将整个活动的布局放在一个滚动视图中,它弥补了动态布局中看不见的子项


  • 谢谢你的代码片段。我应该有哪种布局?我如何控制元素在屏幕上的确切位置?我使用不同的逻辑来获取元素,无论如何,谢谢。我对此也很陌生,所以我尝试了一些东西,我在我的答案中附加了代码,我尝试了动态视图,请探索并修改根据您的需要编写代码,希望对您有所帮助:-)…根据所附的代码,您将了解如何在动态视图中放置内容…控制元素在屏幕上的确切位置完全取决于您如何编写它…您必须按照xml布局中的顺序放置元素
    LinearLayout parentLayout = (LinearLayout) findViewById(R.id.parentLayout);
    TextView ChildView = new TextView(getApplicationContext());
    ChildView.setText("I am the child");
    parentLayout.addView(ChildView);