【问题标题】:Trying to inflate layout in Activity not working尝试在 Activity 中膨胀布局不起作用
【发布时间】:2014-01-21 04:24:33
【问题描述】:

我正在尝试将我制作的自定义视图添加到我的适配器中。我正在尝试这样做以通过 commonsware 添加一个视图来合并适配器。

https://github.com/commonsguy/cwac-merge

这是我试图膨胀布局的代码:

    LayoutInflater inflater = getLayoutInflater();
    LinearLayout row = (LinearLayout) inflater.inflate(R.layout.row,
            null);


        TextView headerOne = (TextView) row
                .findViewById(R.id.titleTextView);
        TextView headerTwo = (TextView) row
                .findViewById(R.id.titleTextView);
        headerOne.setText("One");
        headerTwo.setText("Two");

        mergeAdapter.addView(headerOne);
        mergeAdapter.addAdapter(myArrayAdapter);

        mergeAdapter.addView(headerTwo);
        mergeAdapter.addAdapter(myOtherArrayAdapter);

这是我的 xml。

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_margin="16dp"
    android:layout_weight="1"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/titleTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="sans-serif-condensed"
        android:text="message text"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="@color/text_gray"
        android:textStyle="bold" />

    <View
        android:layout_width="fill_parent"
        android:layout_height="1dip"
        android:background="#000000" />
</LinearLayout>

更新:忘了谈论我的“问题”是什么。首先,我以前从未对布局进行过充气,所以我不确定它是如何工作的/什么是充气的正确方法。其次,我得到这个错误:

01-20 23:18:46.427: E/AndroidRuntime(24810): java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams

编辑:

我的进口:

import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.actionbarsherlock.app.SherlockListActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.commonsware.cwac.merge.MergeAdapter;
import com.google.gson.JsonObject;
import com.koushikdutta.async.future.FutureCallback;
import com.koushikdutta.ion.Ion;

import com.eghdk.myapp.R;
import com.eghdk.myapp.util.AppUtil;
import com.eghdk.myapp.util.DatabaseHelper;

public class MyActivity extends SherlockListActivity {

【问题讨论】:

  • 有什么问题?两个标题都显示"One"?您必须膨胀两个不同的行变量。您正在为两个标题重复使用相同的 row 视图。
  • @A--C 用我的“问题”更新了我的问题对不起!
  • 我指出的是另一个问题,一旦你弄清楚了。对于这个CCE,我在Merge Demo 中看到Button 是使用活动上下文创建的;它没有膨胀。这样做有用吗?
  • 我与图书馆的创建者交谈,他说膨胀和添加视图应该可以工作。

标签: android xml android-layout layout-inflater commonsware-cwac


【解决方案1】:

使用两种布局资源,而不是一种。不要膨胀布局,然后尝试获取该布局的各个部分并单独使用它们。

或者,换句话说,您传递给addView() 的值必须是:

  • 来自inflate()直接结果,或
  • 使用构造函数在 Java 中创建的东西

【讨论】:

    【解决方案2】:

    试试这个...

        LayoutInflater inflater = getLayoutInflater();
    
        LinearLayout row = (LinearLayout) inflater.inflate(R.layout.row, null);
        TextView headerOne = (TextView) row.findViewById(R.id.titleTextView);
        headerOne.setText("One");
        ViewParent parent = row.getParent();
        if(parent != null && parent instanceof ViewGroup) {
            ((ViewGroup)parent).removeView(row);
        }
        AbsListView.LayoutParams layoutParams = new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.WRAP_CONTENT);
        row.setLayoutParams(layoutParams);
    
        mergeAdapter.addView(row);
        mergeAdapter.addAdapter(myArrayAdapter);
    
        row = (LinearLayout) inflater.inflate(R.layout.row, null);
        TextView headerTwo = (TextView) row.findViewById(R.id.titleTextView);
        headerTwo.setText("Two");
        parent = row.getParent();
        if(parent != null && parent instanceof ViewGroup) {
            ((ViewGroup)parent).removeView(row);
        }
        layoutParams = new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.WRAP_CONTENT);
        row.setLayoutParams(layoutParams);
    
        mergeAdapter.addView(row);
        mergeAdapter.addAdapter(myOtherArrayAdapter);
    

    【讨论】:

    • 01-20 23:50:13.699: E/AndroidRuntime(29425): java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams 不能转换为 android.widget.AbsListView$LayoutParams跨度>
    • @EGHDK 您应该将 LinearLayout 的实例添加到合并适配器,而不是 TextView,因为 TextView 是 LinearLayout 的子级并且它具有 LinearLayout.LayoutParams...
    【解决方案3】:

    改变你的下面一行

     LinearLayout row = (LinearLayout) inflater.inflate(R.layout.row,
            null);
    

     View row = inflater.inflate(R.layout.row,
            null);
    

    【讨论】:

    • 试过这个并得到:01-20 23:42:45.505: E/AndroidRuntime(28738): java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams 不能转换为 android.widget。 AbsListView$LayoutParams
    • 请发布更多代码。 @EGHDK 您发布的代码不足以解决您的问题。
    • 我现在包含我的包含语句
    • @EGHDK 如何处理您的导入语句?
    【解决方案4】:

    我想,你没有将view 添加到parent

    试试

    yourParent.addView(row);

    其中yourParent 是您需要附加此row 的布局

    【讨论】:

      【解决方案5】:

      尝试删除以下行。

      LinearLayout row = (LinearLayout) inflater.inflate(R.layout.row, null);
      

      取而代之的是下面的代码。

      View row = inflater.inflate(R.layout.row, null);
      

      【讨论】:

      【解决方案6】:
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多