标签: layout-inflater

如何在android中的gridview适配器的getView()内膨胀另一个布局?

Weekly_Calendar_View 我想创建每周日历视图并在每个网格项目内(每天)可能有几个活动.但是我已经使用网格视图创建了每周日历视图但我想通过动态创建特定日期的活动检查db.和图像一样.下面是我的getView()代码..

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = inflater.inflate(R.layout.calendar_week_gridcell, parent, false);
    }

    txtRowTitle = (TextView) row.findViewById(R.id.txtDayTitle);

    LinearLayout root = (LinearLayout) row.findViewById(R.id.linear_root);

    String dayTitle = list.get(position);
    txtRowTitle.setText(dayTitle);
    if (position == currentWeekDay - 1)
        root.setBackgroundResource(R.drawable.calheader);
    if (!activityMap.isEmpty() && activityMap.containsKey(activityDateList.get(position))) {
        TextView item = new TextView(mContext);
        item.setText(activityMap.get(activityDateList.get(position)));
        item.setBackgroundColor(Color.GREEN);
        root.addView(item);
    }

    return row;
}
Run Code Online (Sandbox Code Playgroud)

}

这里我暂时尝试动态添加文本视图,但我想在这里充气我的自定义activity_item布局并将其添加到网格单元格.

android android-layout android-gridview layout-inflater

8
推荐指数
1
解决办法
2万
查看次数

在Android中扩展视图后,自定义视图子项为null

我正在开发一款Android应用.我有一个自定义视图和布局如下:

<com.hello.view.card.inner.SimpleCardView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/card_simple_linear_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/simple_label"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

</com.hello.view.card.inner.SimpleCardView>
Run Code Online (Sandbox Code Playgroud)

这是Java类:

public class SimpleCardView extends LinearLayout {
    protected SimpleCard card = null;
    protected TextView textView;

    public SimpleCardView(Context context) {
        super(context);     
        init(context);
    }

    public SimpleCardView(Context context, AttributeSet attrs) {
        super(context, attrs);      
        init(context);
    }

    public SimpleCardView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context);
    }

    protected void init(Context context) {
        textView = (TextView)findViewById(R.id.simple_label);
    }

    public void setCard(SimpleCard card) {
        this.card = card;
        textView.setText(card.getMessage());        
    }
}
Run Code Online (Sandbox Code Playgroud)

这就是我如何夸大视图(我尝试了以下两个调用):

SimpleCardView …
Run Code Online (Sandbox Code Playgroud)

java android android-custom-view layout-inflater

8
推荐指数
1
解决办法
2156
查看次数

ViewStub会在有条件地膨胀多个布局时引发错误

在我的应用程序中,我有一个微调器和一个ViewStub,取决于微调器中的用户项目选择,我必须膨胀不同的布局并在微调器下方显示膨胀的布局.当我的应用程序启动时,ViewStub会在第一次从微调器中选择任何项目时成功膨胀布局.当我尝试从微调器中选择一个新项时,它会在下面引发异常

java.lang.IllegalStateException: ViewStub must have a non-null ViewGroup viewParent
Run Code Online (Sandbox Code Playgroud)

到目前为止我的代码是

@Override
public void onItemSelected(AdapterView<?> pParent, View pView, int pPosition, long pId) {

if(pPosition == 1){
    m_cStub.setLayoutResource(R.layout.text_form);
}else if(pPosition == 2){
    m_cStub.setLayoutResource(R.layout.integer_form);
}
View inflated = m_cStub.inflate();
}
Run Code Online (Sandbox Code Playgroud)

m_cStub是在Activity的onCreate()内创建的ViewStub对象.

这是我的主要布局xml代码

<RelativeLayout..................>
     <spinner......................./>

     <ViewStub android:id="@+id/dynamic_form_layout"
    android:inflatedId="@+id/dynamic_form_inflated_id"
    android:layout_alignParentBottom="true"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"/>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

谁能告诉我哪里出错了.如果您有任何其他解决方案可以解决此问题,请分享.

谢谢.

android inflate viewstub layout-inflater

7
推荐指数
1
解决办法
7157
查看次数

你如何在LayoutInflater类的Android的inflate方法中使用第二个参数

所述的充气方法LayoutInflater抽象类具有作为取充气方法的第二个参数ViewGroup根.从文档中,它被称为"可选视图,作为生成的层次结构的父级".

有人可以举例说明如何使用这个参数吗?那你会放在那里?A ViewGroup可以是任何类型的布局LinearLayout.

我还不太明白如何处理这个参数.如果您正在膨胀的视图不是此处输入的布局的一部分,那么它将给出错误.不明白它的目的.

更多来自文档:

public View inflate (XmlPullParser parser, ViewGroup root)

Added in API level 1 Inflate a new view hierarchy from the specified xml node. Throws InflateException if there is an error.

出于性能原因,重要的是,视图通胀在很大程度上依赖于在构建时完成的XML文件的预处理.因此,目前还不能使用LayoutInflaterXmlPullParser过在运行时一个普通的XML文件.

参数解析器包含视图层次结构描述的XML dom节点.

root可选视图是生成的层次结构的父级.返回膨胀层次结构的根视图.如果提供了root,则这是根视图; 否则它是膨胀的XML文件的根.

android android-arrayadapter layout-inflater

7
推荐指数
1
解决办法
3626
查看次数

片段和onConfigurationChanged

我正在尝试用活动做一些事情,但是在一个片段中.我所做的是使用活动:

首先停止旋转设备时重新启动活动 android:configChanges="keyboardHidden|orientation|screenSize"

在我的活动中添加:

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

 setContentView(R.layout.main);
}
Run Code Online (Sandbox Code Playgroud)

因此,获取活动不会重新启动,而是重新加载main.xml,以使用layout-land

现在我有一个显示viewpager的活动,其中包含三个片段.一切正常.旋转的检测在碎片中

public class FRG_map_web extends Fragment  {

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);

        Log.i("myLogs", "Rotation");

    }
Run Code Online (Sandbox Code Playgroud)

问题是片段不使用setContentView(R.layout.main); 这是代码:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.frg.myFragment, null); 
Run Code Online (Sandbox Code Playgroud)

我试着用:

LayoutInflater inflater = inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

view = inflater.inflate(R.layout.frg.myFragment, null); 

...

LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

view = inflater.inflate(R.layout.frg.myFragment, null); 

...

LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );

view = inflater.inflate(R.layout.frg.myFragment, …
Run Code Online (Sandbox Code Playgroud)

android android-fragments layout-inflater

7
推荐指数
3
解决办法
2万
查看次数

无法在Android中以XML格式设置膨胀视图的布局宽度,高度和重量

我正在开发一款Android应用.在我的应用程序中,我需要动态地扩展视图列表.我加了他们并且正在工作.问题是设置布局的宽度和高度.现在我将通过一个简单的项目来演示我的问题.实际上,我的项目比这个简单的项目复杂得多.

我正在夸大这种布局的观点.

           <LinearLayout
                android:orientation="horizontal"
                android:id="@+id/cm_photos_container"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

            </LinearLayout>
Run Code Online (Sandbox Code Playgroud)

我循环遍历位图列表并动态添加视图,如下所示

for(Bitmap bmp : bitmaps)
{
     View preview = layoutInflater.inflate(R.layout.item_cm_preview_image,null);
                ImageView previewImageView = (ImageView)preview.findViewById(R.id.item_cm_preview_image);
                previewImageView.setImageBitmap(bmp);
     container.addView(preview);
}
Run Code Online (Sandbox Code Playgroud)

请注意,在上面的代码中,container是一个LinearLayout动态添加到上面的父XML中.

container = new LinearLayout(this);
                    container.setOrientation(LinearLayout.HORIZONTAL);
                    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                    container.setLayoutParams(params);

parentLinearLayout.addView(container);
Run Code Online (Sandbox Code Playgroud)

这是我的item_cm_preview_image.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:layout_weight="1"
    android:layout_height="400dp"
    android:layout_width="0dp"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <ImageView
        android:scaleType="centerCrop"
        android:id="@+id/item_cm_preview_image"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

如您所见,我在XML中将布局高度设置为400dp,宽度0和layout_weight为1.因此,由于layout_weight,所有图像高度必须相同且宽度必须相等.但结果并不像预期的那样.你可以在下面看到截图.

在此输入图像描述

正如您在屏幕截图中看到的那样,layout_weight和height都不适用于所有膨胀的视图.但是,如果我动态添加额外的ViewGroup并将视图扩展到该布局,它就可以了.以下是我的代码

//This happening in for loop
LinearLayout wrapper = new LinearLayout(this);
                wrapper.setLayoutParams(new LinearLayout.LayoutParams(0,500,1));

                View preview = layoutInflater.inflate(R.layout.item_cm_preview_image,null);
                ImageView previewImageView = (ImageView)preview.findViewById(R.id.item_cm_preview_image);
                previewImageView.setImageBitmap(bmp);

                wrapper.addView(preview);
                container.addView(wrapper); …
Run Code Online (Sandbox Code Playgroud)

android android-layout layout-inflater android-view android-viewgroup

7
推荐指数
1
解决办法
5375
查看次数

错误膨胀类ImageView - android.view.InflateException:二进制XML文件行#10:错误膨胀类ImageView

我正在设计一个应用程序,其中包含列表项的xml文件(list_item.xml),以及listView(NewsAdapter.java)的适配器.

我的应用在运行时遇到此错误:

09-22 20:26:29.595 10387-10387/com.example.rh.newsapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.rh.newsapp, PID: 10387
android.view.InflateException: Binary XML file line #10: Binary XML file line #10: Error inflating class ImageView
    at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
    at com.example.rh.newsapp.NewsAdapter.getView(NewsAdapter.java:38)
    at android.widget.AbsListView.obtainView(AbsListView.java:2346)
    at android.widget.ListView.measureHeightOfChildren(ListView.java:1280)
    at android.widget.ListView.onMeasure(ListView.java:1188)
    at android.view.View.measure(View.java:18788)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
    at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1112)
    at android.widget.LinearLayout.onMeasure(LinearLayout.java:632)
    at android.view.View.measure(View.java:18788)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
    at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
    at android.view.View.measure(View.java:18788)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
    at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:391)
    at android.view.View.measure(View.java:18788)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
    at android.view.View.measure(View.java:18788)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
    at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
    at android.widget.LinearLayout.onMeasure(LinearLayout.java:630) …
Run Code Online (Sandbox Code Playgroud)

android imageview android-layout layout-inflater

7
推荐指数
2
解决办法
1万
查看次数

更好地在Android中扩充或实例化控件?

我想知道是否有人能够对动态创建控件(inflate vs instantiate)的最佳实践有所了解.

膨胀:

TextView styledText = (TextView)inflater.inflate(R.layout.styledTextView);
Run Code Online (Sandbox Code Playgroud)

实例:

TextView styledText = new TextView(mContext);
styledText.setTextAppearance(R.style.StyledTextStyle);
Run Code Online (Sandbox Code Playgroud)

正在创建的对象可以包含膨胀的XML文件中的属性,也可以包含在随后添加到实例化对象的样式定义中.(假设此样式包括宽度,背景,文本颜色等).

无法对每种方法进行任何时间/内存测试,想知道是否有人知道哪种方法最快/最有效.

performance android layout-inflater

6
推荐指数
1
解决办法
1830
查看次数

在Android 4.1上运行应用程序时无法转换为维度:type = 0x1

我最近开始使用genymotion而不是经典的Android虚拟设备,但我有一些问题..当我尝试运行我的应用程序时,我收到此错误.

Can't convert to dimension: type=0x1
Run Code Online (Sandbox Code Playgroud)

我来自LayoutInflater ..当我在Genymotion中运行时,它说有一些布局参数有不好的类型..下面是android studio的两个截图.第一个是在Nexus 4上运行应用程序时使用的,第二个是来自Genymotion. 我在Nexus 4上测试应用程序时已经使用了这个

这个是在Genymotion虚拟设备上运行时拍摄的

两者都应该运行Jelly Bean,唯一的区别是Genymotion是在API 16上,而Nexus 4在4.2.2上运行最新,因此API 17 ..

问题来自我的自定义列表视图适配器 - 来自其getView方法,因此我认为它必须与其中一个布局相关.(我有两种不同类型的列表项)

list_heading.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="wrap_content">


<TextView
        android:id="@+id/listViewHeaderText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="8dp"
        android:paddingTop="10dp"
        android:paddingBottom="3dp"
        android:text="Nacionále"
        android:textAppearance="?android:attr/listSeparatorTextViewStyle"
        android:textColor="@color/main_cvut"/>


<RelativeLayout
        android:id="@+id/listViewHeaderLine"
        android:layout_width="fill_parent"
        android:layout_height="1dip"
        android:background="@color/main_cvut"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/listItem"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/list_item_layout"
            android:clickable="true"
            android:focusable="true"
            android:paddingTop="?android:attr/listPreferredItemPaddingStart"
            android:minHeight="?android:attr/listPreferredItemHeight">

<TextView
        android:id="@+id/itemTitle"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Title"
        android:paddingRight="?android:attr/listPreferredItemPaddingRight"
        android:paddingLeft="?android:attr/listPreferredItemPaddingLeft"
        android:textAppearance="?android:attr/textAppearanceMedium"/>

<TextView
        android:id="@+id/itemDescription"
        android:textColor="@android:color/darker_gray"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="description"
        android:paddingRight="?android:attr/listPreferredItemPaddingRight"
        android:paddingLeft="?android:attr/listPreferredItemPaddingLeft"
        android:textAllCaps="true"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:layout_marginBottom="?android:attr/listPreferredItemPaddingEnd"
        android:paddingBottom="?android:attr/listPreferredItemPaddingRight"
        android:layout_below="@+id/itemTitle"/>

<ImageView …
Run Code Online (Sandbox Code Playgroud)

android android-layout layout-inflater

6
推荐指数
1
解决办法
9623
查看次数

使用片段时应用崩溃

我正在使用片段和..

我的代码中遇到了一个我无法找到的问题.

logcat指向这段代码,在我的一个片段中:

    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    return inflater.inflate(R.layout.activity_main, container, false);
}
Run Code Online (Sandbox Code Playgroud)

我的主要类(FragmentActivity):

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.View;

public class Fragments extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fragments);

        MainActivity fragment = new MainActivity();
        FragmentTransaction transaction = getSupportFragmentManager()
                .beginTransaction();
        transaction.add(R.id.fragment_place, fragment);
        transaction.commit();
    }

    public void onSelectFragment(View view) {

        Fragment newFragment;

        if (view == findViewById(R.id.add)) {
            newFragment = new Add();
        } else if (view == …
Run Code Online (Sandbox Code Playgroud)

android class android-fragments layout-inflater android-fragmentactivity

6
推荐指数
1
解决办法
8443
查看次数