2011-06-28 13 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

ZIPファイルをどこかにダウンロードして共有しますか?ページに「エラー404ファイルが見つかりません」と表示されます。 Thanks –

答えて

4

私はzip形式の情報源に見て、そこにあります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にはこれらのファイルokinkcelbar_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は、ように、1つのボタンを定義する必要があります。

<?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's & 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(); 
    } 
} 
関連する問題