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

huangapple go评论74阅读模式
英文:

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

问题

以下是您提供的内容的翻译:

我无法处理两个相对布局(一个嵌套在另一个中)。我的应用程序无法启动。它一直在停止。我正在使用物理设备(三星 Galaxy On Max)从 Android Studio 运行我的应用程序。

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="下一个"
        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="就是这里!"
            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 文件:

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);
    }
}
英文:

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.

XML file:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
    xmlns:tools=&quot;http://schemas.android.com/tools&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    tools:context=&quot;.MainActivity&quot;&gt;

    &lt;Button
        android:id=&quot;@+id/Next&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_marginStart=&quot;161dp&quot;
        android:layout_marginTop=&quot;340dp&quot;
        android:layout_marginEnd=&quot;162dp&quot;
        android:onClick=&quot;Next&quot;
        android:text=&quot;Next&quot;
        android:visibility=&quot;visible&quot;
        app:layout_constraintEnd_toEndOf=&quot;parent&quot;
        app:layout_constraintStart_toStartOf=&quot;parent&quot;
        app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;

    &lt;androidx.constraintlayout.widget.ConstraintLayout
        android:id=&quot;@+id/newLayout&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;match_parent&quot;
        android:visibility=&quot;invisible&quot;&gt;

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

&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;

Java file:

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

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

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

英文:

Why you are creating object of relative layout while your layout in xml is constraint layout.

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.

答案2

得分: 0

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

英文:

Kindly check on which line of code the app is crashing. Post your error log here !

答案3

得分: 0

问题是你在你的XML布局文件中使用了ConstraintLayout,并在Java文件中创建了RelativeLayout的对象。

将下面这行代码:

RelativeLayout newLayout;

替换为:

ConstraintLayout newLayout;
英文:

problem is you used constraint layout in your xml layout file and creating object of relative layout in java file

replace below line :

RelativeLayout newLayout;

with :

ConstraintLayout newLayout;

huangapple
  • 本文由 发表于 2020年10月16日 14:05:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/64383720.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定