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

Posted

技术标签:

【中文标题】Android 自定义组件:未找到 layout_width 属性【英文标题】:Android custom component: layout_width attribute not found 【发布时间】:2013-01-10 10:43:39 【问题描述】:

我有一个自定义组件。

我总是收到“您必须提供 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_
        android:layout_
        android:text="Deleted"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#fff" />

    <Button
        android:id="@+id/undobar_button"
        android:layout_
        android:layout_
        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_
    android:layout_
    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)

【问题讨论】:

对于初学者来说是&lt;de.myapp.android.UndoBar line 50? 【参考方案1】:

在为 XML 中的自定义视图添加 layout_widthlayout_height 后尝试:

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

【讨论】:

我很困惑。我曾经尝试过这个建议,但直到现在才有效。而且我什至不知道为什么以及为什么 setLayoutParams 不起作用。很抱歉浪费了您的时间。 @cdbeelala89 :因为我也面临这个问题,如果我们不声明 layout_width 或 layout_height 那么它将在运行时出错

以上是关于Android 自定义组件:未找到 layout_width 属性的主要内容,如果未能解决你的问题,请参考以下文章

Android 自定义 SeekBar

Android自定义组件

Joomla 自定义组件未找到 404 错误

按钮未显示在android的自定义列表视图中

自定义复选框显示 2.3 和 4.2 的不同布局

Android 自定义TextView实现文字两端对齐