Android Dynamic RelativeLayout AddRules();没有按预期工作
我试图在服务的相对布局中动态显示一些视图。基本上,我试图让Android Dynamic RelativeLayout AddRules();没有按预期工作,android,android-layout,android-service,android-relativelayout,Android,Android Layout,Android Service,Android Relativelayout,我试图在服务的相对布局中动态显示一些视图。基本上,我试图让文本视图显示在图像视图的右侧,如下所示: ImageView TextView 我实际上得到的是: TextView ImageView (在那个空白处。) 我的变量: private WindowManager windowManager; private ImageView myImage; private TextView myText; private RelativeLay
文本视图
显示在图像视图
的右侧,如下所示:
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中扩展布局,它是有效的,但是,据我所知,我以后无法使用这种方法访问视图 LayoutInflater是正确的选择,您可以访问windowManager中的视图,并且您的服务不会被破坏 你应该像下面这样做
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);
你的quoteWindow是什么…?尝试过?很有趣的工具,但它生成的代码与我已有的代码几乎相同。设置textParams和/或imageParams的边距没有任何效果。将ALIGN_PARENT_TOP移动到新的addRule()会产生与删除它相同的结果:文本显示在图像上。我希望我没有错过任何其他变化。