gpt4 book ai didi

java - Android 自定义组件 : layout_width attribute not found

转载 作者:行者123 更新时间:2023-11-30 03:48:36 24 4
gpt4 key购买 nike

我有一个自定义组件。

我总是收到“您必须提供 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)

最佳答案

在 Xml 中为自定义 View 添加 layout_widthlayout_height 后尝试:

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

关于java - Android 自定义组件 : layout_width attribute not found,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14539257/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com