简体   繁体   English

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

[英]How do i make use of two relative layouts on one single screen?

I cant operate with two relative layouts (one in another).我无法使用两个相对布局(一个在另一个中)进行操作。 my app doesn't starts.我的应用程序没有启动。 it keeps stopping.它不断停止。 I am using a physical device (Samsung Galaxy On Max) for running my apps from android studio.我正在使用物理设备(Samsung Galaxy On Max)从 android studio 运行我的应用程序。

XML file: XML文件:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
    tools:context=".MainActivity">

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

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/newLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="invisible">

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

</androidx.constraintlayout.widget.ConstraintLayout>

Java file: Java文件:

package com.example.twolayouts;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.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);
    }
}

Why you are creating object of relative layout while your layout in xml is constraint layout.为什么要创建相对布局的对象,而 xml 中的布局是约束布局。

Try this :尝试这个 :

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);
}
}

You should try looking into your logcat windows it will help you out for what error you are facing.您应该尝试查看您的 logcat 窗口,它会帮助您解决面临的错误。

Kindly check on which line of code the app is crashing.请检查应用程序崩溃的代码行。 Post your error log here !在此处发布您的错误日志!

problem is you used constraint layout in your xml layout file and creating object of relative layout in java file问题是您在 xml 布局文件中使用了约束布局并在 java 文件中创建了相对布局的对象

replace below line :替换以下行:

RelativeLayout newLayout;相对布局 newLayout;

with :和 :

ConstraintLayout newLayout;约束布局新布局;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM