2011-06-28 60 views
4

只是尋求一些幫助。如果這太模糊,請讓我知道。Android示例,「佈局技巧:合併佈局」不起作用

我想要的「合併佈局」的例子在這裏找到: http://developer.android.com/resources/articles/layout-tricks-merge.html

,我似乎無法得到它的工作。頁面上的源代碼下載不包括所有需要的文件。我在下面粘貼一些代碼,並將其註釋掉。如果這些評論未被評論,我會收到很多錯誤。如果任何人有一個建議之前,我開始粘貼錯誤,那將是巨大的......

OkCancelBar:

package com.example.android.merge; 

import android.content.Context; 
import android.content.res.TypedArray; 
import android.util.AttributeSet; 
import android.view.Gravity; 
import android.view.LayoutInflater; 
import android.widget.Button; 
import android.widget.LinearLayout; 

public class OkCancelBar extends LinearLayout { 
    public OkCancelBar(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     setOrientation(HORIZONTAL); 
     setGravity(Gravity.CENTER); 
     setWeightSum(1.0f); 

     LayoutInflater.from(context).inflate(R.layout.okcancelbar, this, true); 
     /* 
     TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.OkCancelBar, 0, 0); 

     String text = array.getString(R.styleable.OkCancelBar_okLabel); 
     if (text == null) text = "Ok"; 
     ((Button) findViewById(R.id.okcancelbar_ok)).setText(text); 

     text = array.getString(R.styleable.OkCancelBar_cancelLabel); 
     if (text == null) text = "Cancel"; 
     ((Button) findViewById(R.id.okcancelbar_cancel)).setText(text); 

     array.recycle(); 
     */ 

    } 
} 

的main.xml:

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

<merge 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:okCancelBar="http://schemas.android.com/apk/res/com.example.android.merge"> 

    <ImageView 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 

     android:scaleType="center" 
     android:src="@drawable/golden_gate" /> 

    <com.example.android.merge.OkCancelBar 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="bottom" 

     android:paddingTop="8dip" 
     android:gravity="center_horizontal" 

     android:background="#AA000000" 
     <!-- 
     okCancelBar:okLabel="Save" 
     okCancelBar:cancelLabel="Don't save" 
     --> 
     /> 

</merge> 
+0

你能發佈的錯誤?當試圖確定問題時,它會有很大的幫助。 – f20k

+0

你想分享壓縮文件到其他地方供我下載?該頁面顯示「找不到錯誤404文件」。謝謝 –

回答

4

我看着拉鍊源和有沒有res/values/attrs.xml文件。真奇怪。
創建attrs.xml文件,並把下列代碼:

<resources> 
    <declare-styleable name="OkCancelBar"> 
     <attr name="okLabel" format="string" /> 
     <attr name="cancelLabel" format="string" /> 
    </declare-styleable> 
</resources> 

應該現在的工作,但我沒有時間來測試它,對不起。

+0

此外,.zip也缺少這些文件,okcancelbar_button.xml和okcancelbar.xml。 – worked

+0

@Hello Worked你可以給我okcancelbar.xml中我該做什麼,你可以告訴我它會幫助我。 – Herry

+0

這是如此遲鈍,來自官方博客的源代碼示例不起作用.... – philipp

2

在該示例中缺少一對文件。即:

在佈局文件夾下: 它應該有main.xml,okcancelbar.xml和okcancelbar_button.xml。 在值文件夾下: 它應該有attrs.xml

在示例文章中提供了main.xml和okcancelbar.xml的內容。 的okcancelbar_button.xml需要定義一個按鈕:

<?xml version="1.0" encoding="utf-8"?> 
<Button 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/button" 
    android:orientation="vertical" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content">  
</Button> 

而且attrs.xml應提供標籤定義:

<resources> 
    <declare-styleable name="OkCancelBar"> 
     <attr name="okLabel" format="string" /> 
     <attr name="cancelLabel" format="string" /> 
    </declare-styleable> 
</resources> 

然後一切都應該走到一起。

1

pawelzieba的& x射線的答案是正確的也看這個修改。
認沽下面的代碼okcancelbar.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <Button 
     android:id="@+id/okcancelbar_ok" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" > 
    </Button> 

    <Button 
     android:id="@+id/okcancelbar_cancel" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" > 
    </Button> 

</LinearLayout> 


並修改OkCancelBar.java按照下列更好地理解

public class OkCancelBar extends LinearLayout 
{ 
    Context mContext; 

    public OkCancelBar(Context context, AttributeSet attrs) 
    { 
     super(context, attrs); 
     setOrientation(HORIZONTAL); 
     setGravity(Gravity.CENTER); 
     setWeightSum(1.0f); 

     mContext = context; 

     LayoutInflater.from(context).inflate(R.layout.okcancelbar, this, true); 

     TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.OkCancelBar, 0, 0); 

     String text = array.getString(R.styleable.OkCancelBar_okLabel); 
     if (text == null) text = "Ok"; 
     //((Button) findViewById(R.id.button)).setText(text); 
     ((Button) findViewById(R.id.okcancelbar_ok)).setText(text); 

     text = array.getString(R.styleable.OkCancelBar_cancelLabel); 
     if (text == null) text = "Cancel"; 
     //((Button) findViewById(R.id.button)).setText(text); 
     ((Button) findViewById(R.id.okcancelbar_cancel)).setText(text); 

     Button btnCancel = (Button) findViewById(R.id.okcancelbar_cancel); 
     btnCancel.setOnClickListener(new View.OnClickListener() 
     { 
      @Override 
      public void onClick(View v) 
      { 
       Toast.makeText(mContext, "Cancel is Clicked...", Toast.LENGTH_LONG).show(); 
      } 
     }); 

     Button btnOk = (Button) findViewById(R.id.okcancelbar_ok); 
     btnOk.setOnClickListener(new View.OnClickListener() 
     { 
      @Override 
      public void onClick(View v) 
      { 
       Toast.makeText(mContext, "OK is pressed...", Toast.LENGTH_LONG).show(); 
      } 
     }); 

     array.recycle(); 
    } 
}