Android 在自定义视图类中膨胀 xml 布局
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11029192/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Inflating a xml layout in a custom View class
提问by LuminiousAndroid
I have a class View1
that extends View
. I want to inflate R.layout.test2.xml
in this class View1
. I have put a following code in this class
我有一个View1
扩展View
. 我想R.layout.test2.xml
在这门课上膨胀View1
。我在这个类中放置了以下代码
public class View1 extends View {
View view;
String[] countries = new String[] {"India", "USA", "Canada"};
public View1( Context context) {
super(context);
LayoutInflater mInflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view=mInflater.inflate(R.layout.test2, null, false);
}
}
From another class Home
I want this inflated view to be there for some circumstances , In the Home
class I wrote the following code:
在另一个类中,Home
我希望在某些情况下可以使用这种膨胀的视图,在Home
类中我编写了以下代码:
public class Home extends Activity{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
CreateView();
}
public void CreateView() {
LinearLayout lv=(LinearLayout)findViewById(R.id.linearlayout);
View1 view = new View1(Home.this);
lv.addView(view);
}
}
But as I run my project the activity doesn't show me anything.
但是当我运行我的项目时,活动没有显示任何内容。
回答by Luksprog
You can't add views to the View
class instead you should use ViewGroup
or one of its subclasses(like Linearlayout
, RelativeLayout
etc). Then your code will be like this:
您不能添加欣赏到View
类,而不是你应该使用ViewGroup
或它的子类(如之一Linearlayout
,RelativeLayout
等等)。那么你的代码将是这样的:
public class View1 extends LinearLayout {
View view;
String[] countries = new String[] {"India", "USA", "Canada"};
public View1( Context context) {
super(context);
LayoutInflater mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mInflater.inflate(R.layout.test2, this, true);}}
Also the line:
还有这一行:
mInflater.inflate(R.layout.test2, null, false)
doesn't add anything to the current view(because of the false
parameter) , it just inflates a View
from a xml layout.
不会向当前视图添加任何内容(因为false
参数),它只是View
从 xml 布局中膨胀 a 。
回答by Aracem
Use this
用这个
LayoutInflater li = (LayoutInflater)getContext().getSystemService(infService);
li.inflate(R.layout.test2, **this**, true);
You must to use this
, not null, and change the false
parameter (boolean AttachToRoot ) to true
您必须使用this
,而不是 null,并将false
参数(布尔值 AttachToRoot )更改为true
回答by Bharat Sharma
Use the code below to inflate your layout, then you can use that view for any purpose. This will give you the most parent layout of your XML file. Type cast and use it accordingly.
使用下面的代码来扩展您的布局,然后您可以将该视图用于任何目的。这将为您提供 XML 文件的最父布局。键入 cast 并相应地使用它。
View headerView = View.inflate(this, R.layout.layout_name, null);
回答by d.k.
You are adding in home activity blank view. Because in View1 class you have only inflate view but not add anywhere.
您正在添加家庭活动空白视图。因为在 View1 类中,您只有膨胀视图,但不能在任何地方添加。
回答by Marius
You have to use a ViewGroup
like FrameLayout
and do:
您必须使用ViewGroup
喜欢FrameLayout
并执行以下操作:
public class View1 extends FrameLayout {
public View1(Context context) {
super(context);
inflate(context, R.layout.view1, this);
}
}
In the layout XML, use the <merge
tag to not just add your view1
layout to the root layout which means we have an empty FrameLayout
and your defined view beside each other.
在布局 XML 中,使用<merge
标签不仅将您的view1
布局添加到根布局,这意味着我们有一个空的FrameLayout
和您定义的视图并排。
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="content" />
</merge>
See http://trickyandroid.com/protip-inflating-layout-for-your-custom-view/
见http://trickyandroid.com/protip-inflating-layout-for-your-custom-view/
回答by chris
In kotlin LayoutInflater.from(this).inflate(R.layout.custom_toolbar, null, false)
在科特林 LayoutInflater.from(this).inflate(R.layout.custom_toolbar, null, false)