我有这个代码
View item = View.inflate(context, R.layout.item_layout, null);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
layout.addView(item, params);
Run Code Online (Sandbox Code Playgroud)
我的item_layout :(注意部分android:layout_marginTop ="2dip")
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content" android:layout_marginTop="2dip" android:layout_width="fill_parent">
<ImageView android:src="@drawable/pic_unknown" android:id="@+id/image1"
android:layout_height="50dip" android:layout_width="50dip"
android:padding="5dip"></ImageView>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
然后在我的布局中,我看到了项目列表,但它们之间没有任何边距.我尝试使用margintop = 10dip仍然没有发生我的观点是我在布局中放置的值不是在计算中有或没有边距顶部布局是相同的.
如何在项目之间添加一些空白区域?如何在物品之间充气?是否有可能膨胀像空隙或某些空间?或者我必须使用解决方法,比如用2dip高度或某些东西来膨胀一些空布局谢谢
我在Android Lolipop上遇到了崩溃.
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.content.res.StringBlock.get(int)' on a null object reference
at android.content.res.AssetManager.getResourceValue(AssetManager.java:213)
at android.content.res.Resources.getValue(Resources.java:1334)
at android.content.res.Resources.getValue(Resources.java:1323)
at android.content.res.Resources.loadXmlResourceParser(Resources.java:2796)
at android.content.res.Resources.getLayout(Resources.java:1143)
at android.view.LayoutInflater.inflate(LayoutInflater.java:412)
Run Code Online (Sandbox Code Playgroud)
通常在充气视图时发生.有什么建议只在棒棒糖上发生吗?
我有一个简单的应用程序有两个片段,当处于横向模式时,两个片段并排显示,并在纵向我显示片段A,然后如果他们选择一个选项,启动一个显示片段B的活动.我的问题是当我我处于纵向模式并显示片段B,如果用户选择了一个菜单选项,我想要刷新或重绘与片段B关联的视图,并且无法理解如何使其工作.我尝试了getView方法和getLayoutInflater方法,但屏幕没有改变,因为我认为我正在创建一个新视图.我还试图获得对Fragment A的引用,我认为我可以调用它的例程来构建一个新的片段并替换Fragment B但是因为它没有被显示而无法获得对它的引用.我真正需要做的是强制再次调用onCreateView方法来为新视图充气,但我尝试了几种方法来尝试这样做,但是无法再次调用onCreateView.有什么想法怎么样?
我在回收器视图内部创建了一行,我在内部膨胀了两行或更多但是当我滚动时,项目再次被使用.我没有得到回收视图的位置或动态删除它我需要一个提要和他们的评论.对于评论我需要膨胀布局,以显示它们创建getCommentInflaterView方法的feed.
但是使用getCommentInflaterView()创建的视图会重复.MyAdapterClass:
public class AllFeedsAdapter extends
RecyclerView.Adapter<AllFeedsAdapter.ViewHolder> {
ArrayList<Data> arrayListAllFeedsData;
Comment objComment;
Context context;
public AllFeedsAdapter(Context context, ArrayList<Data> data) {
this.context = context;
arrayListAllFeedsData = new ArrayList<>();
arrayListAllFeedsData = data;
}
@Override
public AllFeedsAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
// Inflate the custom layout
View post_row_view = inflater.inflate(R.layout.row_teacher_post, parent, false);
// Return a new holder instance
ViewHolder viewHolder = new ViewHolder(post_row_view);
return viewHolder;
}
@Override
public void onBindViewHolder(AllFeedsAdapter.ViewHolder holder, int position) { …
Run Code Online (Sandbox Code Playgroud) android android-listview layout-inflater android-recyclerview
我正在查看systrace
我的应用程序生成的内容,并确定了一个耗时太长的框架.这是由引起RecyclerView
的onCreateViewHolder
膨胀我的项目视图时.项目视图与posible一样平坦ConstraintLayout
.但是下面的systrace
报告有很多VerifyClass
块,每个块需要17毫秒.
这是什么VerifyClass
块呢?
我已经定义了一个扩展LinearLayout功能的自定义ViewGroup:
public class TestLayout extends LinearLayout {
public TestLayout(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater inflater = (LayoutInflater)context.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.testlayout, this, true);
}
}
Run Code Online (Sandbox Code Playgroud)
它膨胀的布局(testlayout.xml)如下所示:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" android:gravity="center">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
最后我在main.xml中使用这个自定义组件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<my.test.namespace.TestLayout
android:id="@+id/testLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</my.test.namespace.TestLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
在布局编辑器中查看我的main.xml时,eclipse会抛出一个错误: my.test.namespace.TestLayout无法实例化.
和堆栈跟踪:
android.content.res.Resources$NotFoundException: Could not resolve resource value: 0x7F030001.
at com.android.layoutlib.bridge.android.BridgeResources.throwException(BridgeResources.java:648)
at com.android.layoutlib.bridge.android.BridgeResources.getLayout(BridgeResources.java:270)
at android.view.LayoutInflater.inflate(LayoutInflater.java:318)
at my.test.namespace.TestLayout.<init>(TestLayout.java:18) …
Run Code Online (Sandbox Code Playgroud) 我有兴趣创建一个通用的BaseAdapter,它将应用于任何具有任何list_item的ListView.它将为list_view设置item_row.
public class GenericAdapter extends BaseAdapter
{
Context context;
ArrayList<HashMap<String, String>> list;
LayoutInflater inflater;
public GenericAdapter (Context context, ArrayList<HashMap<String, String>> list)
{
this.context = context;
this.list = list;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount()
{
return list.size();
}
@Override
public Object getItem(int position)
{
return list.get(position);
}
@Override
public long getItemId(int position)
{
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View view = convertView;
if(view == null)
{
view = inflater.inflate(R.layout.item_of_any_list, …
Run Code Online (Sandbox Code Playgroud) 我浏览了一些教程,在Android Doc中,它说在实例化时不直接访问LayoutInflater.来自Google Doc的示例:
LayoutInflater inflater = (LayoutInflater)context.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
Run Code Online (Sandbox Code Playgroud)
我经历的教程就是这个:
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
Run Code Online (Sandbox Code Playgroud)
所以除了显而易见的不同代码之外,我真正理解的不同之处在于.任何解释都非常感激.我假设Android Doc应该是我们遵循的那个,但我不确定它是否有所作为.
我有一个视图的XML定义,我将使用addChild添加到更大的容器视图中.它基于a LinearLayout
,看起来基本上是这样的:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="52dip"
android:background="@drawable/box_bg"
android:clickable="true"
android:onClick="onTreeBoxClick"
android:orientation="horizontal" >
<ImageView android:id="@+id/box_photo"
android:layout_width="45dip"
android:layout_height="45dip"
...
/>
<RelativeLayout
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="wrap_content"
>
Run Code Online (Sandbox Code Playgroud)
(省略剩余 - 可能不相关,因为它基本上按设计工作)
当我创建这些视图时,我发现以下对我来说很奇怪的行为:
在我给视图充气后,getLayoutParameters()返回null.
在我调用addChild()将其添加到其父级之后,getLayoutParameters()返回一个有效的对象.
检查LayoutParameters,我发现宽度和高度都设置为-2(WRAP_CONTENT),这显然不是我在XML文件中指定的.
当我查看随附的ImageView的布局参数时,它会以指定的值读出.
谁能解释一下这里发生了什么?为什么我的指定高度没有被注意到?
这并没有真正影响我,因为父视图是一个自定义视图,其中我使用MeasureSpec等强制子项的最终尺寸,但我还是想了解这一点!
我的LayoutInflater.Factory
(下面的代码示例)调用onCreateView
并使用'com.android.support:support-v4:22.0.0'正常工作的主要问题.但是onCreateView
当我转到'com.android.support:support-v4:22.1.0'或更高版本时,不会调用它.我不明白为什么?
//From many fragments i Call hintManager.inflate
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup viewGroup, Bundle savedInstanceState) {
layout = hintManager.inflate(inflater, R.layout.generate_code);
...
//here is HintManager method called from various fragments.
public View inflate(LayoutInflater inflater, int layoutResourceId) {
AttributeParser attributeParser = new AttributeParser();
LayoutInflater layoutInflater = attributeParser.getLayoutInflater(inflater);
final View v = layoutInflater.inflate(layoutResourceId, null);
//here AttributeParserFactory#onCreateView should be called, but it fails with 22.1+ support lib, but works with 22.0
attributeParser.setViewAttribute(v);
return v;
}
...
//example …
Run Code Online (Sandbox Code Playgroud) android ×10
layout-inflater ×10
baseadapter ×1
factory ×1
fragment ×1
inflate ×1
java ×1
margin ×1
systrace ×1
view ×1