【问题标题】:How to make a new layout with custome values xml (java)?如何使用自定义值 xml (java) 创建新布局?
【发布时间】:2017-04-11 09:05:54
【问题描述】:

我正在尝试制作一个自定义 图像 的新布局,R.id.imageStep1。我在layout.addView(guideLayout) 收到错误消息。

这是我的 java 类:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.guide_base);

    ViewGroup layout = (ViewGroup) findViewById(R.id.guide_base);
    ViewGroup guideLayout = (ViewGroup) findViewById(R.id.guide_layout);

    Intent intent = getIntent();

    ImageView image;
    image = (ImageView) findViewById(R.id.guideImage1);

    String guideImage = intent.getStringExtra("guideImage");
    int resId = guideLayout.getResources().getIdentifier(guideImage, "drawable", null);
    image.setImageResource(resId);

    layout.addView(guideLayout);

    String guideTitle = intent.getStringExtra("guideTitle");
    String guideText = intent.getStringExtra("guideText");
}

为什么guideLayout如果我启动为null:

guideLayout = (ViewGroup) findViewById(R.id.guide_layout);

这是添加具有自定义值的新 xml 文件的正确方法吗?

guide_base.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/guide_base">

</LinearLayout>

guide_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    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:id="@+id/guide_layout">

    <ImageView
        android:layout_width="32dp"
        android:layout_height="32dp"
        app:srcCompat="@drawable/one_black_36dp"
        android:layout_marginLeft="8dp"
        android:id="@+id/imageStep1"
        android:contentDescription="Steps to follow"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="6dp" />

    <TextView
        android:text="TextView:"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/titleStep1"
        android:textColor="@color/colorGreenPrimary"
        android:layout_marginBottom="2dp"
        android:layout_marginLeft="4dp"
        android:textSize="20sp"
        android:layout_alignBottom="@+id/imageStep1"
        android:layout_toRightOf="@+id/imageStep1"
        android:layout_toEndOf="@+id/imageStep1"
        android:textStyle="bold"/>

    <TextView
        android:id="@+id/textGuide1"
        android:contentDescription="Guide Step 1"
        android:layout_alignLeft="@+id/titleStep1"
        android:layout_alignStart="@+id/titleStep1"
        android:layout_below="@id/titleStep1"
        android:layout_height="wrap_content"
        android:textColor="@color/colorGreenSecondary"
        android:textSize="16sp"
        android:layout_width="330dp"
        android:text="Si una persona está despierta pero menos alerta de lo usual, hágale una serie de preguntas sencillas, como:  ¿Cuál es su nombre? ¿Qué día es? ¿Cuántos años tiene?" />

    <ImageView
        app:srcCompat="@drawable/persona_desmayada"
        android:id="@+id/guideImage1"
        android:scaleType="centerCrop"
        android:contentDescription="zzz"
        android:layout_height="125dp"
        android:layout_width="145dp"
        android:layout_marginTop="25dp"
        android:layout_below="@+id/textGuide1"
        android:layout_centerInParent="true" />


</RelativeLayout>

LogCat:

java.lang.RuntimeException:无法启动活动 ComponentInfo{com.alertavecino.alertavecino/com.alertavecino.alertavecino.guide}:java.lang.NullPointerException:尝试调用虚拟方法 'android.view.ViewGroup空对象引用上的 $LayoutParams android.view.View.getLayoutParams()'

【问题讨论】:

  • 你能添加你的 XML 文件吗?使用:image = (ImageView) findViewById(R.id.imageview_Main);而不是 image = (ImageView) guideLayout.findViewById(R.id.imageview_Main);
  • 您的 XML 中没有 R.id.guide_base。我不确定我是否理解您要执行的操作。您正在使用 setContentView(R.layout.guide_base);我猜其中没有guide_layout。在这种情况下,它将找不到它并返回 null。请解释您要做什么,我会尝试提出答案。
  • 我确实有一个guide_base.xml,我把它贴出来让你看看。
  • 你需要对 xml 进行膨胀。

标签: java android xml


【解决方案1】:

在 setContentView(R.layout.guide_base);您正在声明活动的布局。在您的布局中,您只有 guide_base 在这种情况下

    ViewGroup guideLayout = (ViewGroup) findViewById(R.id.guide_layout);

将返回 null,因为它不存在于 guide_base 布局中。 为什么不在 guide_base 中包含 guide_layout 布局?

如果您想在其他地方重复使用它。您可以将您的 guide_base 编辑为:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/guide_base">
        <include layout="@layout/guide_layout"
          android:id="@+id/guide_layout"/>
</LinearLayout>

如所述:https://developer.android.com/training/improving-layouts/reusing-layouts.html

或者使用inflator 来创建guide_layout 视图。见How to inflate one view with a layout

【讨论】:

  • 您可以使用@Malik Kawee 回答,以防您选择放大视图。
  • 这让我可以用新的布局给它充气,但是我怎样才能改变 XML 新布局的属性呢?
  • 充气后使用ImageView图片; image = (ImageView) findViewById(R.id.guideImage1);字符串 guideImage = intent.getStringExtra("guideImage"); int resId = guideLayout.getResources().getIdentifier(guideImage, "drawable", null); image.setImageResource(resId);那应该行得通。如果您正在膨胀视图,请使用 image = (ImageView) layout.findViewById(R.id.guideImage1);而是。
【解决方案2】:

尝试使用 LayoutInflater 调用其他视图。

private View view = null ;
Button addbutton = null;
ViewGroup parent = null;
LayoutInflater inflater = null;
inflater= (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
addbutton = (Button)findViewById(R.id.btnAdd);
parent = (ViewGroup) findViewById(R.id.mainxml);


addbutton.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View view) {
    view = inflater.inflate(R.layout.other, null);
    parent.addView(view);
 }
});

试试这个。希望这会有所帮助:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-18
    相关资源
    最近更新 更多