我想以编程方式将ImageView对齐到LinearLayout的右侧。我该怎么办呢。
答案 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
。