2015-07-19 94 views
0

使用此代碼即可。縮放ImageView。 MultiScreen

Resources r = MainActivity.ct.getResources(); 
    float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 200,r.getDisplayMetrics()); 
    View inflatedLayout = getLayoutInflater().inflate(R.layout.list_row_layout, null, false); 
    inflatedLayout.findViewById(R.id.thumbImage).getLayoutParams().width *= px/540; 
    inflatedLayout.findViewById(R.id.thumbImage).getLayoutParams().height *= px/960; 

但我有應用程序崩潰。如何改變我的imageView佈局的寬度和高度?謝謝。

+0

請分享完整的代碼或logcat的 –

+0

您正在編寫這些代碼withing MainActivitiy桑尼是U使用MainActivity.ct.getResources()原因,請檢查Resource的值r = MainActivity.ct.getResources();在調用這個之後,如果它返回的null valur到r app會崩潰NPE的bcz。而是調用上下文的靜態變量來獲取資源,您可以調用Resource r = getResource();和代碼將正常工作 –

+0

是的,謝謝你的回答。我總是得到正常值,而不是空值。但我無法更改佈局大小。如果我在async類中插入這段代碼,下載圖像併爲_thumbImage_創建位圖,那麼代碼將正常工作,但它對我來說不正確:) –

回答

0
Resources r = getResources(); 

使用此代碼如果u R電話WITHING在MainActivity ELSE使用上下文只看重上下文值喜歡這個

Context ct;//in class 
Context ct=MainActivity.this;//in oncreate() menthod 
Resource r =ct.getResource();//in your code 

或在你的情況下,如果你做的環境信息CT作爲

static Context ct;//in class 
ct=MainAcitivity.this;// in oncreate() function 

致電代碼

Resource r=MainAcitivity.ct.getResource(); 

但它不是一個很好的做法,存儲上下文stactic變量,除非只有它最後選擇使用這樣

float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 200,r.getDisplayMetrics()); 
    View inflatedLayout = getLayoutInflater().inflate(R.layout.list_row_layout, null, false); 
    inflatedLayout.findViewById(R.id.thumbImage).getLayoutParams().width *= px/540; 
    inflatedLayout.findViewById(R.id.thumbImage).getLayoutParams().height *= px/960; 
+0

ty,guy)我已經做了,但是這段代碼仍然沒有工作 –

+0

其工作代碼 –

+0

分享代碼在哪裏以及如何做,而不是爲你工作可能很容易爲所有人提供答案,因爲你正在尋找 –