Java 如何在Android的RelativeLayout类中扩展XML布局?
在我的视图包中创建RelativeLayout类 并在类中膨胀XML文件 用于主要活动 但没有任何错误,emulator已停止Java 如何在Android的RelativeLayout类中扩展XML布局?,java,android,android-layout,android-relativelayout,Java,Android,Android Layout,Android Relativelayout,在我的视图包中创建RelativeLayout类 并在类中膨胀XML文件 用于主要活动 但没有任何错误,emulator已停止 import android.content.Context; import android.content.res.AssetManager; import android.util.AttributeSet; import android.view.LayoutInflater; import android.widget.EditText import andro
import android.content.Context;
import android.content.res.AssetManager;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.EditText
import android.widget.RelativeLayout;
import android.widget.TextView;
import net.sibdiet.R;
public class AboutView extends RelativeLayout {
private TextView about_txt;
private AssetManager assets;
public AboutView(Context context) {
super( context );
initializeViews( context );
}
public AboutView(Context context, AttributeSet attrs) {
super( context, attrs );
initializeViews( context );
}
private void initializeViews(Context context) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.aboutus_layout, this);
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
about_txt = (EditText) findViewById(R.id.aboutus_txt);
}
public AssetManager getAssets() {
return assets;
}
}
我需要为aboutus_layout.xml充气
<merge
xmlns:android="http://schemas.android.com/apk/res/android">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/aboutus_txt"
android:layout_width="61dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="16dp"
android:layout_marginTop="16dp"
android:text="@string/about"
android:textColor="@color/Green"
android:textSize="20sp"/>
</RelativeLayout>
</merge>
在主活动中使用自定义视图
main_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#DCDCDC"
tools:context="net.sibdiet.MainActivity">
<net.sibdiet.View.AboutView
android:id="@+id/about_us_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</android.support.constraint.ConstraintLayout>
blahYour帖子似乎包含未正确格式化为代码的代码。请使用“代码”工具栏按钮或CTRL+K键盘快捷键将所有代码缩进4个空格。有关更多编辑帮助,请单击[?]工具栏图标。代码的问题有: 1) 替换
LayoutInflater充气器=(LayoutInflater)context.getSystemService(context.LAYOUT\u充气器\u服务)代码>带有LayoutInflater充气器=LayoutInflater.from(getContext())
2) 从aboutus_layout.xml中删除RelativeLayout
,因为它在那里太多了。使您的视图(AboutView)位于视图层次结构的根