有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在一个屏幕上使用两个相对的布局?

我不能操作两个相对的布局(一个在另一个)。我的应用程序没有启动。它一直停下来。我正在使用物理设备(三星Galaxy On Max)从安卓 studio运行我的应用程序

XML文件:

<?xml version="1.0" encoding="utf-8"?>
<安卓x.constraintlayout.widget.ConstraintLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    xmlns:app="http://schemas.安卓.com/apk/res-auto"
    xmlns:tools="http://schemas.安卓.com/tools"
    安卓:layout_width="match_parent"
    安卓:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        安卓:id="@+id/Next"
        安卓:layout_width="wrap_content"
        安卓:layout_height="wrap_content"
        安卓:layout_marginStart="161dp"
        安卓:layout_marginTop="340dp"
        安卓:layout_marginEnd="162dp"
        安卓:onClick="Next"
        安卓:text="Next"
        安卓:visibility="visible"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <安卓x.constraintlayout.widget.ConstraintLayout
        安卓:id="@+id/newLayout"
        安卓:layout_width="match_parent"
        安卓:layout_height="match_parent"
        安卓:visibility="invisible">

        <TextView
            安卓:id="@+id/nextText"
            安卓:layout_width="wrap_content"
            安卓:layout_height="wrap_content"
            安卓:layout_marginStart="132dp"
            安卓:layout_marginTop="354dp"
            安卓:layout_marginEnd="132dp"
            安卓:background="#F30808"
            安卓:text="There you Go!"
            安卓:textColor="#0B0A0A"
            安卓:textSize="24sp"
            安卓:visibility="visible"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </安卓x.constraintlayout.widget.ConstraintLayout>

</安卓x.constraintlayout.widget.ConstraintLayout>

Java文件:

package com.example.twolayouts;

import 安卓x.appcompat.app.AppCompatActivity;

import 安卓.os.Bundle;
import 安卓.view.View;
import 安卓.widget.Button;
import 安卓.widget.RelativeLayout;
import 安卓.widget.TextView;

public class MainActivity extends AppCompatActivity {
    TextView nextText;
    Button Next;
    RelativeLayout newLayout;

    public void Next(View view)
    {
        Next.setVisibility(View.INVISIBLE);
        newLayout.setVisibility(View.VISIBLE);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        nextText=(TextView)findViewById(R.id.nextText);
        Next=(Button)findViewById(R.id.Next);
        newLayout=(RelativeLayout)findViewById(R.id.newLayout);
    }
}

共 (3) 个答案

  1. # 1 楼答案

    为什么要创建相对布局的对象,而xml中的布局是约束布局

    试试这个:

    public class MainActivity extends AppCompatActivity {
    TextView nextText;
    Button Next;
    ConstraintLayout newLayout;
    
    public void Next(View view)
    {
        Next.setVisibility(View.INVISIBLE);
        newLayout.setVisibility(View.VISIBLE);
    }
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        nextText=(TextView)findViewById(R.id.nextText);
        Next=(Button)findViewById(R.id.Next);
        newLayout=(ConstraintLayout)findViewById(R.id.newLayout);
    }
    }
    

    您应该尝试查看您的logcat窗口,它将帮助您找出所面临的错误

  2. # 2 楼答案

    问题是在xml布局文件中使用了约束布局,并在java文件中创建了相对布局的对象

    替换以下行:

    相对新布局

    与:

    约束新布局

  3. # 3 楼答案

    请检查应用程序正在崩溃的代码行。在这里发布您的错误日志