2015-06-17 71 views
0

以下是我的要求: 1.我有一個主要的線性佈局定義。 2.我從Api獲得一個數組列表。在運行時膨脹自定義線性佈局

現在,我想添加自定義的線性佈局,其中包含1個imageView和1個textView,並將這些值從API數據中提供給這些值。

上述線性佈局將被添加到垂直方向的主線性佈局中。

定製線性佈局類:

public class LandingPageLinearLayout extends LinearLayout{ 

    private ImageView iv_outletInfo_Icon; 
    private TextView tv_outletInfo; 

    public LandingPageLinearLayout(Context context, String tv_value, int iv_drawable_id) { 
     super(context); 
     // TODO Auto-generated constructor stub 
     /* View view = LayoutInflater.from(getContext()).inflate(
        R.layout.landing_page_ll_custom, null);*/ 

     LayoutInflater inflater = (LayoutInflater) context 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
       inflater.inflate(R.layout.landing_page_ll_custom, this, true); 


     iv_outletInfo_Icon = (ImageView) getChildAt(0).findViewById(R.id.imgview_outletInfoIcon); 
     iv_outletInfo_Icon.setBackgroundResource(iv_drawable_id); 

     tv_outletInfo = (TextView) getChildAt(1).findViewById(R.id.tv_outletInfo); 
     tv_outletInfo.setText(tv_value); 



     // this.addView(view); 

    } 

} 

自定義線性佈局的XML佈局:

<?xml version="1.0" encoding="utf-8"?> 
<com.iwillcode.interfaces.LandingPageLinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_margin="1dp" 
    android:gravity="center_vertical" 
    android:padding="14dp" > 

    <ImageView 
     android:id="@+id/imgview_outletInfoIcon" 
     android:layout_width="22dp" 
     android:layout_height="22dp" 
     android:contentDescription="@null" /> 

    <TextView 
     android:id="@+id/tv_outletInfo" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="5dp" 
     android:text="3.5" 
     android:textColor="@color/text_color" 
     android:textSize="12sp" /> 

</com.iwillcode.interfaces.LandingPageLinearLayout> 

稱爲在活動的方法:

private void generateTextViews(int n, LinearLayout mainLinearLayout){ 
     // Dynamic generation of TextViews as per requirement 
     // n = size of arrayList 

     final LandingPageLinearLayout[] myTextViews = new LandingPageLinearLayout[n]; 
     hashMap = new HashMap<String,String>(); 

     //RatingBar Linear Layout 


     for (int i = 0; i < n; i++) { 
      // create a new Custom Linear Layout 

      hashMap = arraylist_outletInfo.get(i); 

      String name = hashMap.get(Config.KEY_NAME); 
      String value = hashMap.get("value"); 

      if(name.equalsIgnoreCase("rating")){ 
       ll_ratingLayout.setVisibility(View.VISIBLE); 
       tv_rating.setText(hashMap.get("value")); 
       ratingBar.setRating(Float.parseFloat(value)); 

      } 

      final LandingPageLinearLayout rowCustomView = new LandingPageLinearLayout(context,value,R.drawable.outlet_icon); 

      // Set text of rowTextView 
      // rowTextView.setText("This is row #" + i+value); 

      // Condition for alternate colors 

      if(n%2==0){ 
       rowCustomView.setBackgroundColor(Color.WHITE); 
       }else{ 
       rowCustomView.setBackgroundColor(Color.parseColor("#f2f2f2")); 
       }   

      // add the textview to the linearlayout 
      mainLinearLayout.addView(rowCustomView); 

      // save a reference to the textview for later 
      // myTextViews[i] = rowTextView; 
     } 


    } 

logcat的:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.goldvip.crownit/com.goldvip.crownit.LandingPageActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class com.goldvip.interfaces.LandingPageLinearLayout 
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2345) 
     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2405) 
     at android.app.ActivityThread.access$800(ActivityThread.java:149) 
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1324) 
     at android.os.Handler.dispatchMessage(Handler.java:102) 
     at android.os.Looper.loop(Looper.java:211) 
     at android.app.ActivityThread.main(ActivityThread.java:5315) 
     at java.lang.reflect.Method.invoke(Method.java) 
     at java.lang.reflect.Method.invoke(Method.java:372) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:941) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:736) 
Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class com.goldvip.interfaces.LandingPageLinearLayout 
     at android.view.LayoutInflater.createView(LayoutInflater.java:616) 
     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743) 
     at android.view.LayoutInflater.inflate(LayoutInflater.java:482) 
     at android.view.LayoutInflater.inflate(LayoutInflater.java:414) 
     at com.goldvip.interfaces.LandingPageLinearLayout.<init>(LandingPageLinearLayout.java:26) 
     at com.goldvip.crownit.LandingPageActivity.generateTextViews(LandingPageActivity.java:1777) 
     at com.goldvip.crownit.LandingPageActivity.onCreate(LandingPageActivity.java:269) 
     at android.app.Activity.performCreate(Activity.java:5933) 
     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105) 
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298) 
     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2405) 
     at android.app.ActivityThread.access$800(ActivityThread.java:149) 
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1324) 
     at android.os.Handler.dispatchMessage(Handler.java:102) 
     at android.os.Looper.loop(Looper.java:211) 
     at android.app.ActivityThread.main(ActivityThread.java:5315) 
     at java.lang.reflect.Method.invoke(Method.java) 
     at java.lang.reflect.Method.invoke(Method.java:372) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:941) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:736) 
Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet] 
     at java.lang.Class.getConstructor(Class.java:531) 
     at java.lang.Class.getConstructor(Class.java:495) 
     at android.view.LayoutInflater.createView(LayoutInflater.java:580) 
     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743) 
     at android.view.LayoutInflater.inflate(LayoutInflater.java:482) 
     at android.view.LayoutInflater.inflate(LayoutInflater.java:414) 
     at com.goldvip.interfaces.LandingPageLinearLayout.<init>(LandingPageLinearLayout.java:26) 
     at com.goldvip.crownit.LandingPageActivity.generateTextViews(LandingPageActivity.java:1777) 
     at com.goldvip.crownit.LandingPageActivity.onCreate(LandingPageActivity.java:269) 
     at android.app.Activity.performCreate(Activity.java:5933) 
     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105) 
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298) 
     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2405) 
     at android.app.ActivityThread.access$800(ActivityThread.java:149) 
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1324) 
     at android.os.Handler.dispatchMessage(Handler.java:102) 
     at android.os.Looper.loop(Looper.java:211) 
     at android.app.ActivityThread.main(ActivityThread.java:5315) 
     at java.lang.reflect.Method.invoke(Method.java) 
     at java.lang.reflect.Method.invoke(Method.java:372) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:941) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:736) 

當活動加載時,應用程序崩潰。

+0

可能我懇請,我的解決方案工作?如果是,請接受我的回答!謝謝! – Karoly

回答

0

,如果你想在你需要的佈局,至少,這需要ContextAttributeSet作爲參數

public LandingPageLinearLayout(Context context, AttributeSet attr) { 
     super(context, attr); 

而且你應該避免構造函數重載View的構造函數的聲明你的自定義視圖。可以爲自定義佈局聲明自定義屬性。

0

希望它能幫助你。

在XML添加新行的xmlns後:安卓= 「http://schemas.android.com/apk/res/android」

- 新線領域:xmlns:應用程序=「HTTP://模式。 android.com/apk/res/YourMainPackageName「

- 清潔並建立項目。

  • 您必須在使用任何庫時添加此項。
1

logcat的說:java.lang.NoSuchMethodException: 所致[類android.content.Context,接口android.util.AttributeSet]

你忘了實現超類的構造函數,即之一: LinearLayout(上下文上下文,AttributeSet attrs)

它被調用,因爲您使用的是XML中的視圖。 確保實現這個兩個構造以及:

的LinearLayout(上下文的背景下,AttributeSet中的ATTRS)

的LinearLayout(上下文的背景下,AttributeSet中的ATTRS,INT defStyleAttr)