2014-12-05 96 views
0

我有一個LinearLayout對象,並希望動態添加圖像對象(它動態地更改其顏色)來動態更改背景圖像。在佈局中添加填充的動態佈局問題

這裏是代碼:

  if (categoryResponse != null && categoryResponse.result != null 
        && categoryResponse.result.length > 0) { 

       int i = 0; 
       category = new TextView[categoryResponse.result.length]; 
       for (Category cat : categoryResponse.result) { 

        category[i] = new TextView(getActivity()); 
        LinearLayout.LayoutParams par = new LinearLayout.LayoutParams(
          LayoutParams.MATCH_PARENT, 
          LayoutParams.WRAP_CONTENT); 

        par.setMargins(0, 10, 0, 0); 
        category[i].setLayoutParams(par); 

        category[i].setGravity(Gravity.CENTER); 
        category[i].setText(cat.name); 
        category[i].setTag(cat.id); 

        category[i].setPadding(0, 10, 0, 10); 
        category[i].setTextSize(25); 
        category[i].setBackgroundResource(R.drawable.category); 
        category[i].setTypeface(Global.AppsFont); 
        category[i].setTextColor(getResources().getColor(
          R.color.white)); 


        main.addView(category[i]); 

        //System.out.println("header textview width = " +category[i].getWidth() + ""); 

        category[i].setOnClickListener(new OnClickListener() { 

         @Override 
         public void onClick(View v) { 
          String id = (String) v.getTag(); 

          for (Entry<String, LinearLayout> entry : channels 
            .entrySet()) { 

           if (entry.getKey().equals(id)) { 
            entry.getValue().setVisibility(
              View.VISIBLE); 
           } else 
            entry.getValue().setVisibility(
              View.GONE); 
          } 
         } 
        }); 
        i++; 

        LinearLayout LLM = null; 
        LLM = new LinearLayout(getActivity()); 

        channels.put(cat.id, LLM); 

        LLM.setOrientation(LinearLayout.VERTICAL); 
        LLM.setVisibility(View.GONE); 

        LinearLayout.LayoutParams LLParamsm = new LinearLayout.LayoutParams(
          LayoutParams.MATCH_PARENT, 
          LayoutParams.MATCH_PARENT); 

        LLParamsm.setMargins(0, 0, 0, 0); 
        LLM.setPadding(0, 0, 0, 0); 
        LLM.setLayoutParams(LLParamsm); 

        LLM.setBackgroundResource(R.drawable.category_bg); 


        main.addView(LLM); 
        LinearLayout LL = null; 

        int j = 0; 
        ChannelResponse channelResponse = JsonUtils 
          .getChannel(cat.id); 

        channelResponseData.put(cat.id, channelResponse); 
        if (channelResponse != null 
          && channelResponse.result != null 
          && channelResponse.result.length > 0) { 

         for (Channel chan : channelResponse.result) { 

          if (j % 2 == 0) { 
           LL = new LinearLayout(getActivity()); 


           LinearLayout.LayoutParams LLParams = new LinearLayout.LayoutParams(
             LayoutParams.MATCH_PARENT, 
             LayoutParams.MATCH_PARENT); 

           LL.setOrientation(LinearLayout.HORIZONTAL); 
           LL.setWeightSum(1); 

           LL.setLayoutParams(LLParams); 
           LLM.addView(LL);          
          } 

          Button chn = new Button(getActivity()); 
          LinearLayout.LayoutParams ladderFLParams = new LinearLayout.LayoutParams(
            LayoutParams.MATCH_PARENT, 
            LayoutParams.MATCH_PARENT, 0.5f); 
          ladderFLParams.setMargins(10, 10, 10, 10); 
          chn.setBackgroundResource(R.drawable.gray_selector); 
          chn.setGravity(Gravity.CENTER); 
          chn.setLayoutParams(ladderFLParams); 
          chn.setText(chan.name); 
          chn.setTag(chan.id); 
          chn.setTextSize(25); 
          chn.setTextColor(getResources().getColor(
            R.color.black)); 
          chn.setTypeface(Global.AppsFont); 
          chn.setOnClickListener(new View.OnClickListener() { 
           public void onClick(View v) { 
            channelIdid = (String) v.getTag(); 
            createDialogBox(v); 
           } 

          }); 

          LL.addView(chn); 
          channel.add(chn); 

          j++; 
         } 

         if (j % 2 != 0) { 

          Button chn = new Button(getActivity()); 
          LinearLayout.LayoutParams ladderFLParams = new LinearLayout.LayoutParams(
            LayoutParams.MATCH_PARENT, 
            LayoutParams.WRAP_CONTENT, 0.5f); 
          ladderFLParams.setMargins(10, 10, 10, 10); 

          chn.setBackgroundResource(R.drawable.gray_selector); 
          chn.setGravity(Gravity.CENTER_HORIZONTAL 
            | Gravity.CENTER_VERTICAL); 
          chn.setLayoutParams(ladderFLParams); 
          chn.setVisibility(View.INVISIBLE); 
          chn.setTextSize(25); 
          chn.setTextColor(getResources().getColor(
            R.color.black)); 
          chn.setTypeface(Global.AppsFont); 

          LL.addView(chn); 
          channel.add(chn); 
         } 
        } 
       } 
      }' 
+0

線性佈局的LLM對象存在問題。當我添加圖像自動添加填充,我也嘗試其他圖像的背景...圖像是九個補丁 – 2014-12-05 13:37:41

+0

問題解決謝謝http://stackoverflow.com/users/2088815/bruce – 2014-12-12 08:02:39

回答

0

9-補丁圖像自動添加內置於9-補丁填充,作爲填充到該視圖。因此,當您致電LLM.setBackgroundResource時致電LLM.setPadding(0, 0, 0, 0)正在被覆蓋。

這裏是DOC約9補丁:http://developer.android.com/guide/topics/graphics/2d-graphics.html#nine-patch

如果您不想填充,你最好的選擇是設計你的9補丁沒有填充,既與繪製和拉伸區換句話說走到圖像的邊緣。

我想你也可以嘗試在setBackgroundResource之後調用setPadding,但我不確定這是否可行。真的,你應該設計你想要的填充背景。

這有道理嗎?

+0

謝謝謝謝謝謝..... .......正常工作...... – 2014-12-06 11:50:29