1

カスタム コンポーネントがあります。

「layout_width 属性を指定する必要があります」というメッセージが常に表示されます。コンポーネントのコンストラクターを通過した後のエラー。多分あなたは私を助けることができます。

コンポーネントのクラス:

public class UndoBar extends LinearLayout {

    private TextView messageTextView;
    private Button undoButton;

    public UndoBar(Context context) {
        this(context, null);
    }

    public UndoBar(Context context, AttributeSet attrs) {
        super(context, attrs);

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

        messageTextView = (TextView) findViewById(R.id.undobar_message);
        undoButton = (Button) findViewById(R.id.undobar_button);

        setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); // here I set the height/width!!
        setOrientation(HORIZONTAL);
        setVisibility(GONE);
        setPadding(4, 4, 4, 4); // todo muss dp sein
        setBackgroundColor(Color.BLUE);
        setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL);
    }

    public Button getUndoButton() {
        return undoButton;
    }

    public TextView getMessageTextView() {
        return messageTextView;
    }

    public void setOnClickListener(OnClickListener l) {
        undoButton.setOnClickListener(l);
    }

}

コンポーネントの XML ファイル:

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

    <TextView
        android:id="@+id/undobar_message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Deleted"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#fff" />

    <Button
        android:id="@+id/undobar_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="30dp"
        android:background="#808080"
        android:drawableLeft="@drawable/ic_revert"
        android:text="Undo"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#fff" />

</merge>

これはコンポーネントが埋め込まれている場所です(ここでコンポーネントの幅/高さを設定しようとしましたが、うまくいきませんでした):

<RelativeLayout 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" >

    ...

    <de.myapp.android.UndoBar
        android:id="@+id/undobar" />

</RelativeLayout>

ログキャット:

01-26 17:48:19.921: E/java.lang.RuntimeException(5832): Binary XML file line #50: You must supply a layout_width attribute.
01-26 17:48:19.921: E/java.lang.RuntimeException(5832): java.lang.RuntimeException: Binary XML file line #50: You must supply a layout_width attribute.
01-26 17:48:19.921: E/java.lang.RuntimeException(5832):     at android.content.res.TypedArray.getLayoutDimension(TypedArray.java:491)
01-26 17:48:19.921: E/java.lang.RuntimeException(5832):     at android.view.ViewGroup$LayoutParams.setBaseAttributes(ViewGroup.java:5631)
01-26 17:48:19.921: E/java.lang.RuntimeException(5832):     at android.view.ViewGroup$MarginLayoutParams.<init>(ViewGroup.java:5773)
01-26 17:48:19.921: E/java.lang.RuntimeException(5832):     at android.widget.RelativeLayout$LayoutParams.<init>(RelativeLayout.java:1154)
01-26 17:48:19.921: E/java.lang.RuntimeException(5832):     at android.widget.RelativeLayout.generateLayoutParams(RelativeLayout.java:992)
01-26 17:48:19.921: E/java.lang.RuntimeException(5832):     at android.widget.RelativeLayout.generateLayoutParams(RelativeLayout.java:70)
01-26 17:48:19.921: E/java.lang.RuntimeException(5832):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:748)
01-26 17:48:19.921: E/java.lang.RuntimeException(5832):     at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
01-26 17:48:19.921: E/java.lang.RuntimeException(5832):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
01-26 17:48:19.921: E/java.lang.RuntimeException(5832):     at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
01-26 17:48:19.921: E/java.lang.RuntimeException(5832):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
01-26 17:48:19.921: E/java.lang.RuntimeException(5832):     at android.app.Activity.setContentView(Activity.java:1881)
01-26 17:48:19.921: E/java.lang.RuntimeException(5832):     at de.innosoft.android.activity.Einsaetze.onCreate(Einsaetze.java:61)
01-26 17:48:19.921: E/java.lang.RuntimeException(5832):     at android.app.Activity.performCreate(Activity.java:5104)
01-26 17:48:19.921: E/java.lang.RuntimeException(5832):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
01-26 17:48:19.921: E/java.lang.RuntimeException(5832):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2258)
01-26 17:48:19.921: E/java.lang.RuntimeException(5832):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2354)
01-26 17:48:19.921: E/java.lang.RuntimeException(5832):     at android.app.ActivityThread.access$600(ActivityThread.java:150)
01-26 17:48:19.921: E/java.lang.RuntimeException(5832):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244)
01-26 17:48:19.921: E/java.lang.RuntimeException(5832):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-26 17:48:19.921: E/java.lang.RuntimeException(5832):     at android.os.Looper.loop(Looper.java:137)
01-26 17:48:19.921: E/java.lang.RuntimeException(5832):     at android.app.ActivityThread.main(ActivityThread.java:5191)
01-26 17:48:19.921: E/java.lang.RuntimeException(5832):     at java.lang.reflect.Method.invokeNative(Native Method)
01-26 17:48:19.921: E/java.lang.RuntimeException(5832):     at java.lang.reflect.Method.invoke(Method.java:511)
01-26 17:48:19.921: E/java.lang.RuntimeException(5832):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
01-26 17:48:19.921: E/java.lang.RuntimeException(5832):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
01-26 17:48:19.921: E/java.lang.RuntimeException(5832):     at dalvik.system.NativeStart.main(Native Method)
4

1 に答える 1

1

追加後、Xml のカスタム ビューを試してlayout_widthくださいlayout_height:

<de.myapp.android.UndoBar
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/undobar" />
于 2013-01-26T17:03:26.260 に答える