简体   繁体   English

Android自定义组件:找不到layout_width属性

[英]Android custom component: layout_width attribute not found

I have a custom component. 我有一个自定义组件。

I always get a "You must supply a layout_width attribute." 我总是得到“您必须提供layout_width属性”。 error after it's gone through the component's constructor. 通过组件的构造函数后发生错误。 Maybe you can help me. 也许你可以帮我。

The component's class: 组件的类:

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);
    }

}

The component's XML file: 组件的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>

This is where the component is embedded (I also tried setting the width/height of the component here, no luck): 这是嵌入组件的位置(我也尝试过在此处设置组件的宽度/高度,没有运气):

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

The logcat: 日志:

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)

try after adding layout_width and layout_height for custom view in Xml : 在Xml中为自定义视图添加layout_widthlayout_height后,请尝试:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 package 'android' / new pacakge 中找不到属性 'layout_width' 的资源标识符 - No resource identifier found for attribute 'layout_width' in package 'android' / new pacakge 如何在Android中修复提供的layout_width属性 - How to fix supply a layout_width attribute in Android 在Android中以编程方式设置EditText的layout_width - Programatically set layout_width of EditText in Android 在Android应用中获取消息:二进制XML文件第2行:您必须提供layout_width属性 - Getting message in android app: Binary XML file line #2: You must supply a layout_width attribute Android 数据绑定 layout_width 和 layout_height - Android Data Binding layout_width and layout_height android:textview将自身调整为错误的layout_width - android:textview adjusts itself to wrong layout_width 如何根据 Android 布局中的 layout_width 指定 layout_height 值? - How can I specify the layout_height value based on the layout_width in Android layout? 二进制 XML 文件第 7 行:您必须提供 layout_width 属性 - Binary XML file line #7: You must supply a layout_width attribute “二进制XML文件行#8:您必须提供layout_width属性”尽管属性已经存在 - “Binary XML file line #8: You must supply a layout_width attribute” Although Attributes Already Exist 二进制 XML 文件第 41 行:您必须提供 layout_width 属性 - Binary XML file line #41: You must supply a layout_width attribute
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM