2012-12-15 43 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