Java 自定义线性布局不显示
我正在做一个自定义布局,但它没有显示,我不知道为什么 下面是定义类的XML文件 最后是类的使用位置Java 自定义线性布局不显示,java,android,android-linearlayout,Java,Android,Android Linearlayout,我正在做一个自定义布局,但它没有显示,我不知道为什么 下面是定义类的XML文件 最后是类的使用位置 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_paren
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<com.example.name.gw2applicaton.SpecializationView
android:id="@+id/view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_vertical">
</com.example.name.gw2applicaton.SpecializationView>
</LinearLayout>
</LinearLayout>
SpecializationView不可见,我不知道为什么。
我在这里做错了什么?这不是自定义视图的工作方式,正如您所尝试的那样。请改用此约定:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<!-- just include the layout you defined else where -->
<include layout="@layout/layout_specialization"/>
</LinearLayout>
此定义现在包括创建自定义视图的xml功能(现在可能适合您)。它将起作用的原因是现在您将属性集或通过xml定义的属性发送给构造函数。因为您没有包含它,所以当在xml中定义时,它不知道如何处理自定义视图,并且您无法访问可以定义为自定义的布局属性。这不是自定义视图的工作方式,因为您正在尝试这样做。请改用此约定:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<!-- just include the layout you defined else where -->
<include layout="@layout/layout_specialization"/>
</LinearLayout>
此定义现在包括创建自定义视图的xml功能(现在可能适合您)。它将起作用的原因是现在您将属性集或通过xml定义的属性发送给构造函数。因为您没有包含它,所以在xml中定义时,它不知道如何处理自定义视图,并且您无法访问可以定义为自定义的布局属性。回答下面的问题,让我知道是否有帮助回答下面的问题,让我知道这是否有助于了解信息,但是显然不能实例化类SpecializationView,只需使用我建议的include选项:)无论如何,您不应该使用布局充气器来充气自定义视图,这看起来非常容易出错,而且似乎不正确感谢您提供的信息,但是显然不能实例化类SpecializationView,只需使用我建议的include选项:)无论如何,您不应该使用布局充气器来充气自定义视图,这看起来非常容易出错,而且似乎不正确
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<!-- just include the layout you defined else where -->
<include layout="@layout/layout_specialization"/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="65dp"
android:layout_height="match_parent">
<Button
android:layout_width="50dp"
android:layout_height="50dp"
android:text="yop2" />
<Button
android:layout_width="50dp"
android:layout_height="50dp"
android:text="yop2" />
</LinearLayout>
public class SpecializationView extends LinearLayout {
/* Programmatic Constructor */
public SpecializationView(Context context) {
super(context);
init(context, null, 0);
}
/* An XML Constructor */
public SpecializationView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs, 0);
}
/* An XML Constructor */
public SpecializationView(Context context, AttributeSet attrs, int resId) {
super(context, attrs, resId);
init(context, attrs, resId);
}
/**
* All initialization happens here!
*/
private void init(Context context, AttributeSet attrs, int resId){
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.layout_specialization, this, true);
}
}