LayoutInflater
在Android中有什么用?
为root studio传递null给了我这个警告:
避免将null作为视图根传递(需要解析膨胀布局的根元素上的布局参数)
它显示为空值getGroupView
.请帮忙.
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context _context;
private List<String> _listDataHeader; // header titles
// child data in format of header title, child title
private HashMap<String, List<String>> _listDataChild;
public ExpandableListAdapter(Context context, List<String> listDataHeader,
HashMap<String, List<String>> listChildData) {
super();
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
}
@Override
public Object getChild(int groupPosition, int childPosititon) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.get(childPosititon);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public …
Run Code Online (Sandbox Code Playgroud) 我有一个用XML定义的布局.它还包含:
<RelativeLayout
android:id="@+id/item"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
Run Code Online (Sandbox Code Playgroud)
我想用其他XML布局文件来扩充这个RelativeView.我可能会根据情况使用不同的布局.我该怎么办?我正在尝试不同的变化
RelativeLayout item = (RelativeLayout) findViewById(R.id.item);
item.inflate(...)
Run Code Online (Sandbox Code Playgroud)
但他们都没有做好.
需要导入什么或如何在活动以外的地方调用布局inflater?
public static void method(Context context){
//this doesn't work the getLayoutInflater method could not be found
LayoutInflater inflater = getLayoutInflater();
// this also doesn't work
LayoutInflater inflater = context.getLayoutInflater();
}
Run Code Online (Sandbox Code Playgroud)
我getLayoutInflater
只能在活动中打电话,这是限制吗?如果我想创建自定义对话框并且我想为它充气视图,或者如果我想要从服务中显示自定义视图的Toast消息,我只有来自服务的上下文我没有任何活动该怎么办?但我想显示自定义消息.
我需要在代码中不在activity类中的地方使用inflater.
我怎样才能做到这一点 ?
该LayoutInflater.inflate
文档是不看好的目的十分清楚我的attachToRoot
参数.
attachToRoot:膨胀的层次结构是否应附加到根参数?如果为false,则root仅用于为XML中的根视图创建LayoutParams的正确子类.
有人可以更详细地解释,特别是根视图是什么,并且可能显示一个行为true
和false
值之间的变化的例子?
我放在setHasOptionsMenu(true)
里面onCreateView
,但我仍然无法调用onCreateOptionsMenu
内部碎片.
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
setHasOptionsMenu(true);
return inflater.inflate(R.layout.facesheet, container, false);
}
Run Code Online (Sandbox Code Playgroud)
以下是我的onCreateOptionsMenu
代码.
@Override
public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
getSupportMenuInflater().inflate(R.menu.layout, menu);
return (super.onCreateOptionsMenu(menu));
}
Run Code Online (Sandbox Code Playgroud)
我收到的错误消息:
该方法
onCreateOptionsMenu(Menu)
类型片段的必须重写或实现的超类型方法.
android android-fragments layout-inflater oncreateoptionsmenu
我可以在Android中更改菜单项的背景颜色吗?
如果有人对此有任何解决方案,请告诉我.最后一个选项显然是要自定义它,但是有没有办法在不自定义的情况下更改文本颜色.
Avoid passing null as the view root
当用null
as 膨胀视图时,我得到了lint警告parent
,例如:
LayoutInflater.from(context).inflate(R.layout.dialog_edit, null);
Run Code Online (Sandbox Code Playgroud)
但是,视图是用作一个内容AlertDialog
,使用setView
on AlertDialog.Builder
,所以我不知道应该传递什么parent
.
parent
在这种情况下你认为应该是什么?
我有一个水平LinearLayout
包含a TextView
后面跟着Spinner
它.这LinearLayout
在a中LinearLayout
包含的固定垂直中动态膨胀多次RelativeLayout
.
问题是,自从我切换Theme.light
到Theme.holo.light
,最后一行TextView
被削减了一半.当动态文本很长并且跨越多行时会发生这种情况.
我已经能够通过添加底部填充到LinearLayout
包含TextView
和的水平来解决这个问题Spinner
.
这不是一个修复,而是更多的黑客攻击.有人可以就如何正确解决这个问题给我一些建议吗?
我还读了一些其他问题,但似乎都没有帮助.
水平线性布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/textView1"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:text="TextView"/>
<Spinner
android:id="@+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
上面布局在线性布局上以id ll2_7动态膨胀的相对布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ScrollView
android:id="@+id/scrollView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/relLayoutButtonNext"
android:layout_below="@id/textView1" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="20dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp" >
<TextView
android:id="@+id/textView10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="30dp"
android:text="2.7" …
Run Code Online (Sandbox Code Playgroud) 我的问题是:
android ×10
layout-inflater ×10
android-view ×2
layout ×1
menuitem ×1
service ×1
textcolor ×1
textview ×1
toast ×1
warnings ×1