2015-02-24 194 views
0

我有兩個imageviews,我想創建一個邊界像效果,每當用戶點擊它們。 我已將這兩個圖像的背景設置爲彩色。現在,當用戶點擊圖像時,我在imageview上設置了一些填充,以便它創建一個類似效果的邊框。但不幸的是,它沒有發生。Android:setpadding不能使用imageview

這是我的Java代碼:

public void imageClick(View view) 
{ 
switch(view.getId()) 
{ 
case (R.id.imageView1): 
    image1.setPadding(10,10,10,10); 
    image2.setPadding(0,0,0,0); 
    break; 
case (R.id.imageView2): 
    image1.setPadding(0,0,0,0); 
    image2.setPadding(10,10,10,10); 
    break; 
} 
} 

我不明白爲什麼單擊圖像時什麼也沒有發生。我曾經嘗試都

android:cropToPadding = "true" 

android:cropToPadding = "false" 

但是也沒有結果。

我對imageViews XML代碼:

<ImageView 
       android:layout_width="0dp" 
       android:id="@+id/imageRegion1" 
       android:layout_height="match_parent" 
       android:layout_weight="1" 
       android:onClick="imageClick" 
        android:cropToPadding="false" 
        android:background="#fffff300" /> 

       <ImageView 
        android:id="@+id/imageRegion2" 
        android:layout_width="0dp" 
        android:layout_height="match_parent" 
        android:layout_weight="1" 
        android:onClick="imageClick" 
        android:cropToPadding="false" 
        android:background="#fffff300" /> 

我被動態地設置imageviews:

image1.setImageBitmap(bitmap1); 
image2.setImageBitmap(bitmap2); 
+0

變化的android:layout_height = 「match_parent」 到Android:layout_height = 「WRAP_CONTENT」 它應該工作完全 – 2015-02-24 10:54:54

+0

確定生病試試。但是如果我想要「match_parent」而不是wrap_content呢? – nfsleo07 2015-02-24 11:09:26

+0

其應用填充總根視圖。所以它不影響爲不純 – 2015-02-24 11:12:39

回答

0

,因爲它似乎你是最有可能使用weightSum & layout_weight機制同樣計算的ImageView寬度或合理地。

如果我在這裏,在這種情況下,當你設置填充時,它不會在已經膨脹的視圖中做任何改變。正如您可能預期的那樣,設置填充值會降低寬度&高度。但它不會。因爲,由於重量計算,它將採用先前的寬度/高度值,因此將忽略填充。

我希望這會幫助你解決問題。

0
<LinearLayout 
    android:id="@+id/row_view" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:visibility="visible" > 

    <ImageView 
     android:id="@+id/imageRegion1" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:layout_weight="1" 
     android:background="#fffff300" 
     android:cropToPadding="false" 
     android:onClick="imageClick" /> 

    <ImageView 
     android:id="@+id/imageRegion2" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:layout_weight="1" 
     android:background="#fffff300" 
     android:cropToPadding="false" 
     android:onClick="imageClick" /> 
</LinearLayout> 

我試着用這個above.its工作的罰款me.try再次