關於android中怎麼設定TabWidget中圖片及字型的大小的問題

來源:互聯網
上載者:User

由於在所做的項目中的圖片資源都是從網上摳的,所以造成了,在TabWidget中圖片過大,從而覆蓋掉了裡面的文字,最開始的效果

  在網上眾多的搜尋結果中,網上大部分將的都是有關怎麼更改字型的大小及顏色的問題,代碼如下 設定tabHost 中的tabs的字型顏色、大小等;
        TabWidget tabWidget=this.getTabWidget();
        for (int i = 0; i < tabWidget.getChildCount(); i++) {
            TextView tv=(TextView)tabWidget.getChildAt(i).findViewById(android.R.id.title);
            tv.setGravity(BIND_AUTO_CREATE);
            tv.setPadding(10, 10,10, 10);
            tv.setTextSize(16);//設定字型的大小;
            tv.setTextColor(Color.WHITE);//設定字型的顏色;
                //擷取tabs圖片;
            ImageView iv=(ImageView)tabWidget.getChildAt(i).findViewById(android.R.id.icon);
        } 根據上面的例子我們估計會採用兩種方式來解決的問題1、for (int i = 0; i < tabWidget.getChildCount(); i++) {
         
              //擷取tabs圖片;
          ImageView iv=(ImageView)tabWidget.getChildAt(i).findViewById(android.R.id.icon);
          iv.setMaxHeight(30);
          iv.setMaxWidth(30);
      }這樣我們發現還是沒有一點效果2、for (int i = 0; i < tabWidget.getChildCount(); i++) {
         
              //擷取tabs圖片;
          ImageView iv=(ImageView)tabWidget.getChildAt(i).findViewById(android.R.id.icon);
          iv.setLayoutParams(new LayoutParams(width, height));
      }
但這種情況的話,程式會出錯,這是為什麼呢,因為在new LayoutParams(width, height)的時候一定要用父控制項的LayoutParams,而我們在導包的時候並沒有發現有適合的在組織匯入的時候,所有的結果發現無論到那個都會出錯,3、所以我們必須尋求另外一種方法 for(int i=0;i < tabs.getChildCount();i++)
        {
         TextView textView = (TextView)tabs.getChildAt(i).findViewById(android.R.id.title);
         textView.setTextSize(14);
         textView.setPadding(0, 3, 0, 0);
         ImageView image = (ImageView)tabs.getChildAt(i).findViewById(android.R.id.icon);
       
         image.getLayoutParams().height = 30;//通過給它的屬性賦值的方法可以解決問題
         image.getLayoutParams().width = 30;
        }結果完成最初設定的目標

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.