Android动态RelativeLayout相互重叠

标签 android android-layout

我使用此代码通过按钮单击事件动态打印数据库中的值。
用于删除数据库条目的按钮单击事件存在于循环内。

这是我的代码:

    RelativeLayout rl = (RelativeLayout) findViewById(R.id.relativeLayout3);

    final DatabaseHandler dbpin = new DatabaseHandler(this);
  //  Log.d("Reading: ", "Reading all tasks..");
    List<Detail> detail1 = dbpin.getAllDetail();       
    Button[] button=new Button[1000];
            for (Detail cn : detail1) {
                String log = cn.getTitle();
           final  int i = cn.getID();

           button[i] = new Button(this);
       button[i].setText("Delete");
       button[i].setTextSize(10);   

       button[i].setId(2000 + i);
     int width = 80;
     int height = 60;

     TextView textview = new TextView(this);
     textview.setText(log);
     textview.setWidth(200);
     textview.setTextSize(20);
     textview.setPadding( 0, 10, 0, 0);
     textview.setId(2000 + i);

     if (i == 0) {
         RelativeLayout.LayoutParams rlp2 = new RelativeLayout.LayoutParams(
                 RelativeLayout.LayoutParams.FILL_PARENT,
                 RelativeLayout.LayoutParams.WRAP_CONTENT);
         rlp2.addRule(RelativeLayout.ALIGN_PARENT_TOP);
         rlp2.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
         textview.setLayoutParams(rlp2);
         rl.addView(textview);
         RelativeLayout.LayoutParams rlp1 = new RelativeLayout.LayoutParams(
                 width, height);
         rlp1.addRule(RelativeLayout.ALIGN_PARENT_TOP);
         rlp1.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
         button[i].setLayoutParams(rlp1);
         rl.addView(button[i]);
     } else {
         RelativeLayout.LayoutParams rlp2 = new RelativeLayout.LayoutParams(
                 RelativeLayout.LayoutParams.FILL_PARENT,
                 RelativeLayout.LayoutParams.WRAP_CONTENT);
         rlp2.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
         rlp2.addRule(RelativeLayout.BELOW, button[i].getId() - 1);
         textview.setLayoutParams(rlp2);
         rl.addView(textview);
         RelativeLayout.LayoutParams rlp1 = new RelativeLayout.LayoutParams(
                 width, height);
         rlp1.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
         rlp1.addRule(RelativeLayout.BELOW, button[i].getId() - 1);
         button[i].setLayoutParams(rlp1);
         rl.addView(button[i]);
     }


     button[i].setOnClickListener(new Button.OnClickListener() {
        public void onClick(View v) {


            Intent myIntent = new Intent(v.getContext(), details.class);
            Detail detail = new Detail();
            detail.setID(i);
            dbpin.deleteDetail(detail);
             startActivityForResult(myIntent, 1);        
        }
        });                     
 }

在数据库处理程序代码之后,使用循环从数据库检索所有详细信息:

// Getting All detail
                    public List<Detail> getAllDetail() {
                        List<Detail> detailList = new ArrayList<Detail>();
                        // Select All Query
                        String selectQuery = "SELECT  * FROM " + TABLE_DETAIL;

                        SQLiteDatabase db = this.getWritableDatabase();
                        Cursor cursor = db.rawQuery(selectQuery, null);

                        // looping through all rows and adding to list
                        if (cursor.moveToFirst()) {
                            do {
                                Detail detail = new Detail();
                                detail.setID(Integer.parseInt(cursor.getString(0)));
                                detail.setTitle(cursor.getString(1));
                                detail.setDetail(cursor.getString(2));

                                // Adding contact to list
                                detailList.add(detail);
                            } while (cursor.moveToNext());
                        }

                        // return contact list
                        return detailList;
                    }

// Deleting single detail
                    public void deleteDetail(Detail detail) {
                        SQLiteDatabase db = this.getWritableDatabase();
                        db.delete(TABLE_DETAIL, KEY_DETID + " = ?",
                                new String[] { String.valueOf(detail.getID()) });
                        db.close();
                    }

一开始布局是正常的。删除第一个或最后一个数据行不会导致任何变化,但如果删除中间的一行,则布局会相互重叠。

请给我建议来清除这个逻辑错误。

最佳答案

好的,我明白你的问题了。问题是您使用相对布局作为父布局,在其中添加所有子相对布局。现在,如果您删除第一个相对布局,那么它会自动与其父级对齐,因此不会有问题。

如果删除最后一个相对布局,也不会出现问题。

现在您已经将所有相对布局与上面的布局对齐,因此如果您删除上面的布局,它会自动与其父级对齐。

解决方案很简单。使用您的父布局作为线性布局,这样您就不需要将相对布局与其上面的布局对齐。它会自动以线性方式排列......

RelativeLayout rl = (RelativeLayout) findViewById(R.id.relativeLayout3);在 xml 文件中将此布局转换为线性布局。

这是可以帮助您的代码:

    LinearLayout lp = (LinearLayout) findViewById(R.id.LinearLayout1);

//for循环从这里开始

    RelativeLayout rl = new RelativeLayout(getApplicationContext());
    RelativeLayout.LayoutParams lp_btn = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
                                                                     LayoutParams.WRAP_CONTENT);
    lp_btn.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
    Button temp_button = new Button(getApplicationContext());
    temp_button.setText("button");
    rl.addView(temp_button, lp_btn);
    TextView tv = new TextView(getApplicationContext());
    tv.setText("bharat");
    tv.setBackgroundColor(Color.GREEN);
    RelativeLayout.LayoutParams lp_tv = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
                                                                     LayoutParams.WRAP_CONTENT);
    lp_tv.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
    rl.addView(tv, lp_tv);
    lp.addView(rl);

//for循环到此结束

我认为你应该使用listview来达到你的目的,它会更好。无论如何,这也可以工作,您必须为了您的目的管理相对布局数组和按钮数组。

关于Android动态RelativeLayout相互重叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10185368/

相关文章:

android - CoordinatorLayout + CollapsingToolbarLayout 在状态栏下方滚动

java - 如何扩展ConnectionService?

android - 将滑动标签与工具栏一起使用

android - 不正确的用户界面 - android

android - 长按 EditText 崩溃 : Unable to add window

android - 如何删除 MaterialCardView 边框

android - adb和多个设备同时进行

android - Android 应用程序在卸载和重新安装后会记住其数据

java - 按下后退按钮时使用 MainActivity 登录 Activity

android - 相对布局错误中的线性布局