Android的动态RelativeLayout的相互重叠 [英] Android dynamic RelativeLayout overlaps each other

查看:597
本文介绍了Android的动态RelativeLayout的相互重叠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用这个code从我与buttonClick事件数据库中动态打印vaules。

该buttonClick事件删除的数据库条目是一个循环内present。

I use this code to dynamically print the vaules from my database with a buttonClick-event.
The buttonClick-event to delete the database entry is present inside the a loop.

下面我code:

    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);        
        }
        });                     
 }

继数据库处理程序code是使用一个循环来检索数据库中的所有细节:

Following the database handler code is, to retrieve all details from database using a loop:

// 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();
                    }

起初的布局是正常的。删除第一个或最后一个数据行不引起任何改变,但是,如果在中间行被删除,则布局重叠彼此

At first the layout is normal. Deleting first or the last data row doesn't cause any change, but if a row in the middle is deleted, then the layout overlaps each other.

请给我建议,以清除这个逻辑上的错误。

Please give me suggestions to clear this logical error.

推荐答案

好吧,我已经明白你的问题。问题是,你正在使用相对布局为您的父母布局,其中添加的所有子相对布局。现在,如果你删除你的第一个相对布局,然后它会自动与母公司保持一致所以不会有问题。

Ok I have understand your problem. Problem is that you are using relative layout as your parent layout in which you are adding all your child relative layouts. Now if you delete your first relative layout then it automatically align with its parent so there will be no problem.

如果您删除最后一个相对布局则也会发生没有问题。

If you delete last relative layout then also not problem occurs.

现在你必须调整你的所有相对布局形成了上面的布局,所以如果你删除上面之一,它自动对齐到其父。

Now you have align all your relative layout form their above layout so if you delete above one it automatically aligns to its parent.

解决方法很简单。用你的父布局,线性布局,使你不需要调整相对布局与上面的布局。它会以线性的方式自动排列....

Solution is simple. Use your parent layout as linear layout so that you dont need to align relative layouts with their above layout. It will automatically arrange in a linear way....

RelativeLayout的RL =(RelativeLayout的)findViewById(R.id.relativeLayout3);这个转换布局的LinearLayout在XML文件中。

RelativeLayout rl = (RelativeLayout) findViewById(R.id.relativeLayout3); convert this layout in linearlayout in your xml file.

这在code,它可以帮助你:

This the code which can help you:

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

//从这里开始循环

// for loop start from here

    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循环将在这里结束

// for loop will end here

我认为你应该使用你的目的列表视图会更好。反正这也将工作,你必须管理你的目的RelativeLayout的阵列和阵列按钮

I think you should use listview for your purpose it will be better. Anyway this will also work you have to manage relativelayout array and button array for your purpose.

这篇关于Android的动态RelativeLayout的相互重叠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆