如何将ImageView与LinearLayout的右侧以编程方式对齐android

时间:2012-01-06 20:20:23

标签: android

我想以编程方式将ImageView对齐到LinearLayout的右侧。我该怎么办呢。

2 个答案:

答案 0 :(得分:24)

你没有。这不是线性布局的作用。请改用RelativeLayout

XML

<RelativeLayout android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello, World!"
        android:layout_alignParentRight="true"/>
</RelativeLayout>

编程

public void makeView(Context context) {
    RelativeLayout rl = new RelativeLayout(context);
    TextView tv = new TextView(context);
    tv.setText("Hello, World");

    RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams
            (LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);

    rl.addView(tv, lp);
}

答案 1 :(得分:5)

你可以试试这个:

ImageView view = (ImageView)findViewById(R.id.imageView);
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)view.getLayoutParams();
params.gravity = Gravity.LEFT;

但您应该使用RelativeLayout代替LinearLayout

相关问题