膨胀视图与膨胀元素
我想充气 R.id.catText
,但如果我自行充气,它永远不会显示。如果我膨胀 R.id.assets
(容器),那么两个元素都会正常显示。我只是不想要这个容器。如何在不膨胀 R.id.assets
的情况下膨胀 R.id.catText
?
另外,我可以膨胀 R.id.catText
到一个对象吗?例如。
TextView catText = inflater.inflate(R.id.catText);
someView.addView(new catText());
我的代码:
LinearLayout myRoot = (LinearLayout) findViewById(R.id.assets);
LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.assets, myRoot, false);
我的风格:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1"
android:padding="50px" android:id="@+id/assets">
<TextView android:id="@+id/catText" android:text="TextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#ff0000"></TextView>
</LinearLayout>
I would like to inflate R.id.catText
, but it doesn't ever show if I inflate it by itself. If I inflate R.id.assets
(the container) then both elements show up just fine. I just don't want the container. How can I inflate R.id.catText
without inflating R.id.assets
?
Also, can I inflate R.id.catText
to an object? eg.
TextView catText = inflater.inflate(R.id.catText);
someView.addView(new catText());
my code:
LinearLayout myRoot = (LinearLayout) findViewById(R.id.assets);
LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.assets, myRoot, false);
my style:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1"
android:padding="50px" android:id="@+id/assets">
<TextView android:id="@+id/catText" android:text="TextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#ff0000"></TextView>
</LinearLayout>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
LayoutInflater
仅适用于R.layout.*
。如果您只想膨胀TextView
,则应将其放入其自己的布局 XML 文件中。如果您有时需要容器,但其他时候不需要,则可以将TextView
包含在容器布局中。在 catText.xml 中:
在容器 xml 文件中:
然后您可以膨胀您需要的任何一个。
The
LayoutInflater
works onR.layout.*
only. If you want to inflate just theTextView
, you should put it in its own layout XML file. If you need the container sometimes but not others, you can include theTextView
in the container layout.In catText.xml:
In the container xml file:
Then you can inflate whichever one you need.
从 XML 布局创建 View 对象实际上并不需要 inflate 调用。您应该使用 findViewByID 来初始化视图,即本例中的 TextView。
您能解释一下您到底想对 TextView 使用 inflate 调用吗?
Creating View objects from the XML layouts doesn't really need a inflate call. You should be using findViewByID for initializing Views, ie, TextView in this case.
Could you explain for what exactly you are trying to use an inflate call for the TextView?