2015-11-19 75 views
0

我在StackOverflow的任何地方查找它並且似乎無法找到我的問題的答案。下面提供了我正在運行API v.16設備,以及使用Drawable作爲背景的TextView的後臺更新方法。其他的工作 - TextViews成功改變了它們的textSize和height/width,在這裏未提到的部分代碼。任何想法可能是什麼問題?該應用程序不會停止,只有中風的厚度沒有變化。實際上,TextView完全放棄了其背景。它的原始背景是一個特定筆畫寬度的角平滑矩形,它的大小和筆畫寬度應該減半。更改後沒有背景在TextView中一起顯示。設置可以繪製到TextView的背景不起作用

if (textViewsArrayList.size() != 0) textViews.get(textViewsArrayList.size() - 1).post(new Runnable() { 

     @Override 
     public void run() { 

      for (counter = 0; counter < textViewsArrayList.size(); counter++) { 

       textViewsArrayList.get(counter).getLayoutParams().height = (int)    
       (textViewsArrayList.get(counter).getLayoutParams().height/2); 

       textViewsArrayList.get(counter).getLayoutParams().width = (int) (textViewsArrayList.get(counter).getLayoutParams().width/2); 
       ((TextView) textViewsArrayList.get(counter)).setTextSize(TypedValue.COMPLEX_UNIT_PX, (int) (((TextView) textViewsArrayList.get(counter)).getTextSize()/2)); 

       GradientDrawable background = (GradientDrawable) textViewsArrayList.get(counter).getBackground(); 

       background.setStroke((int) (4/displayMetrics.density/2), (int) Color.parseColor("#FFA500"));; 

       if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { 

        ((TextView) textViewsArrayList.get(counter)).setBackground((Drawable) background); 

       } else { 

        ((TextView) textViewsArrayList.get(counter)).setBackgroundDrawable((Drawable) background); 

       } 

      } 

     } 

}); 

雖然有問題的TextView中的XML是:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/layout" 
    android:layout_width="320dp" 
    android:layout_height="90dp" 
    android:tag="layout"> 
    <TextView 
     android:id="@+id/textview" 
     android:layout_height="68dp" 
     android:layout_width="match_parent" 
     android:background="@drawable/drawable_xml" 
     android:layout_marginLeft="6dp" 
     android:layout_marginRight="6dp" 
     android:layout_marginTop="7dp" 
     android:tag="textview_in question"/> 

etc. 

至於提拉XML:

<?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android"> 

    <stroke 
     android:width="4dp" 
     android:color="#FF6600" /> 

    <corners 
     android:bottomRightRadius="10dp" 
     android:bottomLeftRadius="10dp" 
     android:topLeftRadius="10dp" 
     android:topRightRadius="10dp"/> 

    <solid android:color="#FFFFFF" /> 

</shape> 
+0

從您的xml中刪除android:background =「@ drawable/drawable_xml」 。只從java代碼 – Anjali

+0

設置背景嘿,當我檢查你的代碼時,你的代碼是完美的,我也回答了你的問題。 –

+0

@ Anjali:試過了,但沒有奏效。不管怎麼說,還是要謝謝你。 – Dmitri

回答

1

試試這個,

設置背景繪製動態添加到TextView:

TextView txtHello = new TextView(MainActivity.this); 
txtHello.setLayoutParams(lparams); 
txtHello.setText("Hello World"); 
txtHello.setTextSize(14); 
txtHello.setTextColor(Color.parseColor("#9C9C9C")); 
txtHello.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_launcher, null, null, null); 

設置背景繪製到一個TextView使用XML:

<TextView 
    android:id="@+id/textview" 
    android:layout_width="match_parent" 
    android:layout_height="68dp" 
    android:layout_marginLeft="6dp" 
    android:layout_marginRight="6dp" 
    android:layout_marginTop="7dp" 
    android:background="@drawable/rounded_corner" 
    android:textColor="@android:color/black" 
    android:textColorHint="@android:color/black" 
    android:textSize="16sp" /> 

把下面的xml文件到您的繪製文件夾:

rounded_corner.xml

<?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
    <stroke 
     android:width="4dp" 
     android:color="#FF6600" /> 
    <corners 
     android:bottomLeftRadius="10dp" 
     android:bottomRightRadius="10dp" 
     android:topLeftRadius="10dp" 
     android:topRightRadius="10dp" /> 
    <solid android:color="#FFFFFF" /> 
</shape> 
+0

謝謝,但我想要的是動態設置它,因爲它是用於調整不同設備的佈局,我不能看到它發生,我可能會爲應用程序中的每一件事物輸入一個值,這將是災難性的。 – Dmitri

+0

嘿,我已經更新了代碼,請仔細檢查一下,我已經將其加入到我的項目中,並且工作正常,請檢查它。 –

+1

@ Parth:謝謝,您的回覆幫助我解決了這個問題。這暗示了我的觀點可能存在問題。的確,我的textview有match_parent,而不是layout_width的固定值。我想這是以某種方式導致問題,可能與系統請求父級的listview項目(match_parent)的大小來處理背景可繪製變化的時間。我改變父母大小(列表視圖項的大小)的事實可能是問題,因爲在系統確定新值之前可能發生了這種情況。 – Dmitri