繁体   English   中英

将动态创建的RelativeLayout与LinearLayout类内部对齐

[英]Align dynamically created RelativeLayout to right inside LinearLayout Class

我有一个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);

我从此链接获得的: android:layout_gravity的Java方法

有任何想法吗?

我为此苦苦挣扎了一段时间,因此遇到另一个我想发布我的解决方案。 我使用setGravity(Gravity.RIGHT)将图像包装在LinearLayout中,并使用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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM