2012-12-15 40 views
0

我:變化的ImageView重力的RelativeLayout代碼

<ImageView 
    android:id="@+id/logo" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:layout_centerVertical="true" 
    android:contentDescription="@string/logo" 
    android:onClick="LogoAnimate" 
    android:src="@drawable/logo" /> 

我需要改變的:

android: layout_centerHorizontal = "true" 
android: layout_centerVertical = "true" 

到:

android: layout_alignParentBottom = "true" 
android: layout_alignParentLeft = "true" 

我怎樣才能在實現這個碼?

回答

1

LayoutParams開始:

RelativeLayout.LayoutParams params = 
    new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 
            RelativeLayout.LayoutParams.WRAP_CONTENT); 
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE); 
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE); 
params.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.FALSE); 
params.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.FALSE); 

ImageView img1 = (ImageView) findViewById(R.id.logo); 
img1.setLayoutParams(params): 
+1

其實所有4'params.addRule'行代碼是錯誤的。正確的語法是'public void addRule(int verb,int anchor)',但這可用於對齊ID設置爲錨點的某個對象。對於這個片段只有左參數就足夠了。此外,沒有'RelativeLayout.TRUE'和'RelativeLayout.FALSE'常量存在。 – Stan

-4

試試這個

ImageView image= (ImageView)findViewById(R.id.logo); 
    image.setLayoutParams(new RelativeLayout.LayoutParams(width, height, Gravity.BOTTOM | Gravity.LEFT)); 
+0

這是錯誤的 - ''RelativeLayout.LayoutParams'沒有這樣的構造函數。可用的構造函數爲:'RelativeLayout.LayoutParams(上下文C,的AttributeSet ATTRS)'' RelativeLayout.LayoutParams(INT瓦特,INT 1H)'' RelativeLayout.LayoutParams(ViewGroup.LayoutParams源)'' RelativeLayout.LayoutParams(ViewGroup.MarginLayoutParams來源)' – Stan

+0

這是一個錯誤的信息。 – Soham