【发布时间】: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