Android Dynamic RelativeLayout AddRules();没有按预期工作

时间:2015-09-18 05:12:57

标签: android android-layout android-service android-relativelayout

我试图在服务的相对布局中动态显示一些视图。基本上,我想让TextView显示ImageView的权利,如下所示:

ImageView TextView 

我实际得到的是:

TextView                        ImageView 

(那个空的空间。)

我的变数:

private WindowManager windowManager;
private ImageView myImage;
private TextView  myText;
private RelativeLayout myRelativeLayout;
private RelativeLayout.LayoutParams imageParams;
private RelativeLayout.LayoutParams textParams;
private WindowManager.LayoutParams relativeLayoutParams;

服务的onCreate方法:

    windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

    myImage= new ImageView(this);
    myImage.setId(R.id.myImage);

    myText= new TextView(this);
    myText.setId(R.id.myText);

    myRelativeLayout= new RelativeLayout(this);
    myRelativeLayout.setId(R.id.myRelativeLayout);


    final WindowManager.LayoutParams windowManagerParams = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.TYPE_PHONE,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
            PixelFormat.TRANSLUCENT);

    textParams= new RelativeLayout.LayoutParams(
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.WRAP_CONTENT
    );

    imageParams = new RelativeLayout.LayoutParams(
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.WRAP_CONTENT
    );

    relativeLayoutParams= new WindowManager.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.TYPE_PHONE,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
            PixelFormat.TRANSLUCENT
    );

   imageParams.addRule(
   RelativeLayout.ALIGN_PARENT_LEFT | 
   RelativeLayout.ALIGN_PARENT_TOP, myRelativeLayout.getId()); 
   //Tried without getId(),too
  //If I omit ALIGN_PARENT_TOP here, the views are shown on top of each other

   textParams.addRule(RelativeLayout.RIGHT_OF, myImage.getId());

    windowManager.addView(myRelativeLayout, layoutParams);
    myRelativeLayout.addView(myImage, imageParams);
    myRelativeLayout.addView(myText, textParams); 

    // This does not appear to have any significance. 
    windowManager.updateViewLayout(myRelativeLayout, layoutParams);

视图ID在XML文件中被绑定。试图让它在API 14上运行。最后的谜题将有更多的视图,所以我更倾向于坚持相对布局。

我已经阅读了大部分关于这个主题的内容,但我无法弄清楚我做错了什么。我也试过设置重力属性,但没有运气。我还需要稍后处理点击事件。我已经尝试过从XML中扩展布局,但它可以工作,但据我所知,我以后无法使用此方法访问视图。

2 个答案:

答案 0 :(得分:0)

LayoutInflater是正确的选项,您可以访问windowManager中的视图,并且您的服务不会被破坏!

答案 1 :(得分:0)

你应该这样做,

    windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

    myImage= new ImageView(this);
    myImage.setId(R.id.myImage);

    myText= new TextView(this);
    myText.setId(R.id.myText);

    myRelativeLayout= new RelativeLayout(this);
    myRelativeLayout.setId(R.id.myRelativeLayout);


    final WindowManager.LayoutParams windowManagerParams = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.TYPE_PHONE,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
            PixelFormat.TRANSLUCENT);

    textParams= new RelativeLayout.LayoutParams(
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.WRAP_CONTENT
    );

    imageParams = new RelativeLayout.LayoutParams(
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.WRAP_CONTENT
    );

    relativeLayoutParams= new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT,
            RelativeLayout.LayoutParams.TYPE_PHONE,
            RelativeLayout.LayoutParams.FLAG_NOT_FOCUSABLE,
            PixelFormat.TRANSLUCENT
    );

   imageParams.addRule(
   RelativeLayout.ALIGN_PARENT_LEFT);//RelativeLayout.ALIGN_PARENT_TOP 
   imageParams.addRule(
   RelativeLayout.ALIGN_PARENT_TOP);
   //Tried without getId(),too
  //If I omit ALIGN_PARENT_TOP here, the views are shown on top of each other
   textParams.addRule(RelativeLayout.RIGHT_OF, myImage.getId());
   textParams.setMargins(0, 0, 0, 0);

    windowManager.addView(myRelativeLayout, layoutParams);
    myRelativeLayout.addView(myImage, imageParams);
    myRelativeLayout.addView(myText, textParams); 

    // This does not appear to have any significance. 
    windowManager.updateViewLayout(myRelativeLayout, layoutParams);