2014-08-29 105 views
1

我有一個應用程序,我在其中生成一個相對佈局,其中包含兩個文本視圖,並在其下方生成線性佈局。我希望該佈局能夠位於相對佈局的頂部,但沒有任何事情發生。請幫我整理一下。我的代碼片段如下。如何在TextView上動態添加LinearLayout

RelativeLayout relativeLayout = null; 

      LinearLayout relativeLayout1 = null; 

      holder.text_data_for_Name.removeAllViews(); 
      for (Region object : temp.phonelist.regionList) 

      { 

       relativeLayout1 = new LinearLayout(getContext()); 
       relativeLayout1.setOrientation(LinearLayout.VERTICAL); 

       relativeLayout = new RelativeLayout(getContext()); 

       RelativeLayout.LayoutParams relativeLayoutParams; 

       RelativeLayout.LayoutParams lprams = new RelativeLayout.LayoutParams(
         RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 

       final TextView tv2 = new TextView(getContext()); 

       TextView tv1 = new TextView(getContext()); 
       tv1.setText(object.getCCInfoLeft()); 
       lprams.setMargins(0, 0, 300, 0); 
       tv1.setLayoutParams(lprams); 

       tv1.setGravity(Gravity.LEFT); 
       tv1.setTextSize(TypedValue.COMPLEX_UNIT_SP,15); 
       tv1.setPadding(13, 5, 5, 5); 
       tv1.setId(1); 
       relativeLayout.addView(tv1); 

       RelativeLayout.LayoutParams newParams = new RelativeLayout.LayoutParams(
         RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 


        tv2.setText(object.getCCInfoRight()); 
        // newParams.addRule(RelativeLayout.RIGHT_OF, 1); 
        newParams.setMargins(0, 0, 0, 0); 
        newParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); 

        tv2.setLayoutParams(newParams); 
        tv2.setGravity(Gravity.RIGHT); 
        tv2.setTextSize(TypedValue.COMPLEX_UNIT_SP,15); 
        tv2.setPadding(5, 5, 20, 5); 
        tv2.setId(2); 
        relativeLayout.addView(tv2); 





       textView[2] = new TextView(getContext()); 

      relativeLayoutParams = new RelativeLayout.LayoutParams(
        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
      textView[2].setText(""); 

      textView[2].setText(object.getCCInfoShortDesc()); 
      //textView[2].setGravity(Gravity.RIGHT); 
      textView[2].setTextSize(TypedValue.COMPLEX_UNIT_SP,10); 
       String layoutDetail = object.getCCInfoShortDescAlign(); 
      if (layoutDetail.equals("BL")) { 
       //relativeLayout1.setPadding(left, top, right, bottom); 
       linearLayout.setPadding(13, 5, 5, 20); 
       linearLayout.setGravity(Gravity.BOTTOM | Gravity.LEFT); 
       relativeLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, relativeLayout.getId()); 
       linearLayout.addView(textView[2]);//, relativeLayoutParams); 

       relativeLayout.addView(linearLayout); 
      } else if (layoutDetail.equals("BR")) { 
       linearLayout.setPadding(5, 5, 13, 20); 
       linearLayout.setGravity(Gravity.BOTTOM | Gravity.RIGHT); 
       relativeLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, relativeLayout.getId()); 
       linearLayout.addView(textView[2]); 
       relativeLayout.addView(linearLayout); 
      } else if (layoutDetail.equals("TL")) 
      { 
       linearLayout.setPadding(13, 5, 5, 20); 

       relativeLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, relativeLayout.getId()); 

       linearLayout.setGravity(Gravity.LEFT); 

       linearLayout.addView(textView[2]); 
       relativeLayout.addView(linearLayout); 

      } else if (layoutDetail.equals("TR")) { 
       linearLayout.setPadding(5, 5, 13, 20); 
       linearLayout.setGravity(Gravity.TOP | Gravity.RIGHT); 
       linearLayout.addView(textView[2]);//, relativeLayoutParams); 
       relativeLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, relativeLayout.getId()); 
       linearLayout.addView(textView[2]); 
       relativeLayout.addView(linearLayout); 
      } 

      holder.tex 

t_data_for_Name.addView(relativeLayout);

+0

嘗試通過前兩個textViews創建線性佈局和命名的LinearLayout的LinearLayout,而不是relativeLayout1 – Nepster 2014-08-29 07:28:17

+0

2個textViews是內部的LinearLayout或外部.. – Nepster 2014-08-29 07:39:41

+0

@ Nepster相對佈局 – user3683036 2014-08-29 09:17:33

回答

1

試試這種方式,希望這會幫助你解決你的問題。

 RelativeLayout relativeLayout = null; 
     LinearLayout linearLayout = null; 

     int linearId=11; 
     holder.text_data_for_Name.removeAllViews(); 
     for (Region object : temp.phonelist.regionList) { 

      relativeLayout = new RelativeLayout(getContext()); 
      linearLayout = new LinearLayout(getContext()); 
      linearLayout.setOrientation(LinearLayout.VERTICAL); 
      linearLayout.setId(linearId++); 

      textView[2] = new TextView(getContext()); 
      textView[2].setText(""); 
      textView[2].setText(object.getCCInfoShortDesc()); 
      textView[2].setTextSize(TypedValue.COMPLEX_UNIT_SP, 10); 

      RelativeLayout.LayoutParams linearLayoutprams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
      RelativeLayout.LayoutParams tv1prams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
      RelativeLayout.LayoutParams tv2prams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 

      final TextView tv2 = new TextView(getContext()); 
      TextView tv1 = new TextView(getContext()); 
      tv1.setText(object.getCCInfoLeft()); 
      tv1prams.setMargins(0, 0, 300, 0); 
      tv1.setGravity(Gravity.LEFT); 
      tv1.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15); 
      tv1.setPadding(13, 5, 5, 5); 
      tv1.setId(linearId++); 


      tv2.setText(object.getCCInfoRight()); 
      tv2prams.setMargins(0, 0, 0, 0); 
      tv2.setGravity(Gravity.RIGHT); 
      tv2.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15); 
      tv2.setPadding(5, 5, 20, 5); 
      tv2.setId(linearId++); 

      String layoutDetail = object.getCCInfoShortDescAlign(); 
      if (layoutDetail.equals("BL")) { 
       //relativeLayout1.setPadding(left, top, right, bottom); 
       linearLayout.setPadding(13, 5, 5, 20); 
       linearLayout.setGravity(Gravity.BOTTOM | Gravity.LEFT); 
       linearLayoutprams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); 
       linearLayout.addView(textView[2]);//, relativeLayoutParams); 
       relativeLayout.addView(linearLayout); 
       tv1prams.addRule(RelativeLayout.ABOVE,linearLayout.getId()); 
       tv1prams.addRule(RelativeLayout.ALIGN_LEFT); 
       tv2prams.addRule(RelativeLayout.ABOVE,linearLayout.getId()); 
       tv2prams.addRule(RelativeLayout.ALIGN_RIGHT); 
      } else if (layoutDetail.equals("BR")) { 
       linearLayout.setPadding(5, 5, 13, 20); 
       linearLayout.setGravity(Gravity.BOTTOM | Gravity.RIGHT); 
       linearLayoutprams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); 
       linearLayout.addView(textView[2]); 
       relativeLayout.addView(linearLayout); 
       tv1prams.addRule(RelativeLayout.ABOVE,linearLayout.getId()); 
       tv1prams.addRule(RelativeLayout.ALIGN_LEFT); 
       tv2prams.addRule(RelativeLayout.ABOVE,linearLayout.getId()); 
       tv2prams.addRule(RelativeLayout.ALIGN_RIGHT); 
      } else if (layoutDetail.equals("TL")) { 
       linearLayout.setPadding(13, 5, 5, 20); 
       linearLayoutprams.addRule(RelativeLayout.ALIGN_PARENT_TOP); 
       linearLayout.setGravity(Gravity.LEFT); 
       linearLayout.addView(textView[2]); 
       relativeLayout.addView(linearLayout); 
       tv1prams.addRule(RelativeLayout.BELOW,linearLayout.getId()); 
       tv1prams.addRule(RelativeLayout.ALIGN_LEFT); 
       tv2prams.addRule(RelativeLayout.BELOW,linearLayout.getId()); 
       tv2prams.addRule(RelativeLayout.ALIGN_RIGHT); 
      } else if (layoutDetail.equals("TR")) { 
       linearLayout.setPadding(5, 5, 13, 20); 
       linearLayout.setGravity(Gravity.TOP | Gravity.RIGHT); 
       linearLayout.addView(textView[2]);//, relativeLayoutParams); 
       linearLayoutprams.addRule(RelativeLayout.ALIGN_PARENT_TOP); 
       linearLayout.addView(textView[2]); 
       relativeLayout.addView(linearLayout); 
       tv1prams.addRule(RelativeLayout.BELOW,linearLayout.getId()); 
       tv1prams.addRule(RelativeLayout.ALIGN_LEFT); 
       tv2prams.addRule(RelativeLayout.BELOW,linearLayout.getId()); 
       tv2prams.addRule(RelativeLayout.ALIGN_RIGHT); 
      } 
      tv2prams.addRule(RelativeLayout.RIGHT_OF,tv1.getId()); 
      tv1.setLayoutParams(tv1prams); 
      relativeLayout.addView(tv1); 

      tv2.setLayoutParams(tv2prams); 
      relativeLayout.addView(tv2); 
     } 
+0

內的線性佈局@ Haresh Chhelana外面我更新我的問題plzz看到它,有一定的條件下,我必須設置佈局 – user3683036 2014-08-29 07:46:26

+0

現在它只給我佈局頂部,但我也想在底部 – user3683036 2014-08-29 07:53:10

+0

我不清楚你的要求現在。 – 2014-08-29 08:46:53

0

我認爲你需要設置relativeLayoutParams爲relativeLayout1。

relativeLayout1.setLayoutParams(relativeLayoutParams) 

希望它的工作。

相關問題