如何在我的布局上膨胀视图?

发布于 2025-01-07 16:32:55 字数 343 浏览 0 评论 0原文

如何在布局上膨胀视图,其中布局上的所有 ui 元素都应该可见。 我正在尝试这条线,但它不起作用,我无法看到我的背景图像、按钮和其他图像。

MyView view1=new MyView(this);

LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
 view1= (MyView) inflater.inflate(R.layout.main, null);

请帮忙..提前致谢。

How to inflate View on layout, where all my ui elements which are on layout should be visible.
Am trying this lines but it is not working am not able to see my background image, button and other images.

MyView view1=new MyView(this);

LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
 view1= (MyView) inflater.inflate(R.layout.main, null);

Please help.. Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

み格子的夏天 2025-01-14 16:32:55

对于您正在做的事情,使用

setContentView(R.layout.main);

膨胀后未获取视图的原因是您需要在当前视图中的某处添加该视图。目前,您的膨胀视图是孤立的。

For what you are doing, use

setContentView(R.layout.main);

The reason for not getting your view after inflating is that you need to add that View in the current View somewhere. Currently your inflated view is orphaned.

匿名。 2025-01-14 16:32:55

这通常有效...

LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
 View view1= inflater.inflate(R.layout.main, null);

或者

 LayoutInflater inflater = LayoutInflater.from(getApplicationContext);
View view1= inflater.inflate(R.layout.main, null);

然后将此视图添加到 contentView 中。

更新:

假设您需要在 main.xml 中画一条线,让我给您这个建议。不要通过扩展 View 类在运行时画线,而是像这样在 xml 中创建一个 View

<View android:layout_width = "fill_parent"
      android:layout_height = "2dp"  //assuming you want line of 2dp thickness
      android:background= "@android:color/black"/> // define your desired color here 

希望它有所帮助。

this usually works...

LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
 View view1= inflater.inflate(R.layout.main, null);

or

 LayoutInflater inflater = LayoutInflater.from(getApplicationContext);
View view1= inflater.inflate(R.layout.main, null);

and then add this view to the contentView.

Updated:

Assuming you have need to draw a line in your main.xml, let me give you this suggestion. instead of drawing line at runtime by extending View class, create a View in xml like this

<View android:layout_width = "fill_parent"
      android:layout_height = "2dp"  //assuming you want line of 2dp thickness
      android:background= "@android:color/black"/> // define your desired color here 

hope it helps..

他不在意 2025-01-14 16:32:55

这终于奏效了。

getWindow().addContentView(view1,
                new ViewGroup.LayoutParams(
                         ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT));

This worked finally.

getWindow().addContentView(view1,
                new ViewGroup.LayoutParams(
                         ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文