我有一个LinearLayout类,我有: TexView | ImageView | EditText | ImageView的。 我想将最后一个ImageView对齐到它所包含的LinearLayout的右侧。现在它刚刚放在EditText之后,而不是右对齐。 LinearLayout位于ListView内部。对于ImageView,我想右对齐,我将其包装在RelativeLayout中并设置:ALIGN_PARENT_RIGHT。这是我在我的LinearLayout类中尝试右对齐我的最后一个ImageView:
public class CheckBoxifiedTextView extends LinearLayout implements OnClickListener
{
////LAYOUT FOR THIS CLASS///////
setLayoutParams(new ListView.LayoutParams(ListView.LayoutParams.FILL_PARENT, ListView.LayoutParams.WRAP_CONTENT));
this.setVerticalGravity(android.view.Gravity.CENTER_VERTICAL);
this.setOrientation(HORIZONTAL);
///////////////////
//ok now add grabber image:
RelativeLayout.LayoutParams lp_imgGrabber = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lp_imgGrabber.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
m_imgGrabber = new ImageView(context);
m_imgGrabber.setImageResource(R.drawable.grabber);
//TypedArray a=context.obtainStyledAttributes(R.styleable.TouchListView);
//int grabberId = a.getResourceId(R.styleable.TouchListView_grabber, -1);
//m_imgGrabber.setId(2131230808);
RelativeLayout rel_layout_temp = new RelativeLayout(context);
rel_layout_temp.setId(2131230808);
rel_layout_temp.addView(m_imgGrabber);
addView(rel_layout_temp,lp_imgGrabber);
}
我也试过了:
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(WRAP_CONTENT,WRAP_CONTENT,Gravity.RIGHT | Gravity.CENTER_VERTICAL)
addView(m_imgGrabber,params);
我从这个链接获得: Java method for android:layout_gravity
任何想法?
答案 0 :(得分:1)
我挣扎了一段时间,所以遇到这个的其他人我想发布我的解决方案。我将我的图像包装在LinearLayout中:setGravity(Gravity.RIGHT)并使用FILL_PARENT作为LinearLayout参数的宽度:
LinearLayout.LayoutParams ll_params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
m_imgGrabber = new ImageView(context);
m_imgGrabber.setImageResource(R.drawable.grabber);
LinearLayout ll = new LinearLayout(context);
ll.setId(2131230808);
ll.setGravity(Gravity.RIGHT);
ll.addView(m_imgGrabber);
addView(ll,ll_params);