英文:
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:
<?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:
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;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论