【问题标题】:Android Dynamic RelativeLayout AddRules(); not working as expectedAndroid 动态相对布局 AddRules();没有按预期工作
【发布时间】:2015-09-18 05:12:56
【问题描述】:

我试图在服务的相对布局中动态显示一些视图。基本上,我试图让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 扩展布局,并且它可以工作,但是据我了解,以后我无法使用此方法访问视图。

【问题讨论】:

  • 你的quoteWindow是什么......?
  • 试过this ?
  • 有趣的工具,但它生成的代码与我已有的几乎相同。

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


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    你应该像下面那样做,

        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);
    

    【讨论】:

    • 为 textParams 和/或 imageParams 设置边距无效。将 ALIGN_PARENT_TOP 移动到新的 addRule() 会产生与删除它相同的结果:文本显示在图像上。我希望我没有错过任何其他更改。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-18
    • 1970-01-01
    • 2017-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多