0

我必须显示从数据库中获取的图片网格。根据用户的屏幕尺寸,我想显示更多或更少的图片。

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

1a) 哪种布局合适?

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

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

我使用的是 API 版本 10,因此 GridLayout 不可用。

任何建议都会非常有帮助。提前致谢。

4

2 回答 2

1

1) How to change the number of pictures shown dynamically according to the number of pics from the database?

就这部分而言,根据数据库中可用图片的数量使用 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);

}

您可能已经知道如何获取数据库中的元素数量,如果没有,我使用了这个

    // Method 3: Getting total number of entries present in the database
    public int getTotalNumberOfEntries() {
        String[] columns = new String[]{ KEY_ROWID, KEY_TYPE, KEY_DATE};

        Cursor c = myDataBase.query(DATABASE_TABLE, columns, null, null, null, null, null);
        int count = 0;

        for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
                count++;
            }
        c.close();
        return count;
    }

========== 编辑 =================

您可以在活动的 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);    
}
}
于 2012-06-11T12:30:06.430 回答
0
  1. 我通过使用 LinearLayout 解决了这个问题。我使用以下方法动态添加视图:

    LinearLayout parentLayout = (LinearLayout) findViewById(R.id.parentLayout);
    TextView ChildView = new TextView(getApplicationContext());
    ChildView.setText("I am the child");
    parentLayout.addView(ChildView);
    

    我按照 Sumit 的建议循环到可用的视图。

  2. 我不得不将整个活动的布局封装到一个 ScrollView 中,它弥补了动态布局中不可见的子项

于 2012-12-13T10:23:57.753 回答