简体   繁体   English

如何为不同的活动使用相同的 XML 布局?

[英]how can I use same XML layout for different activities?

I am new to android development.I want to use same layout file for two different activities.here is my code我是 android 开发的新手。我想对两个不同的活动使用相同的布局文件。这是我的代码

public class MainActivity extends AppCompatActivity {
ToggleButton toggleButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    toggleButton=findViewById(R.id.toggle_button);
  
    toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {

               on();
            } 
        }
    });
}
public void on()
{

    Toast.makeText(this, "button on", Toast.LENGTH_SHORT).show();
} 
}

second activity第二项活动

public class MainActivity2 extends AppCompatActivity {
ToggleButton toggleButton;
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    textView=findViewById(R.id.text_view);
toggleButton=findViewById(R.id.toggle_button);
    toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {

                textView.setText("hello MainActivity2");
            } 
        }
    });
}
} 

My layout 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">
<TextView
    android:id="@+id/text_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="100dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    tools:ignore="MissingConstraints" />
<ToggleButton
    android:id="@+id/toggle_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    tools:ignore="MissingConstraints" />
</androidx.constraintlayout.widget.ConstraintLayout>

here no text is coming on if i checked the button only toast is coming.如果我检查了按钮,这里没有文字出现,只有吐司来了。 I am searching for an answer to this.我正在寻找这个问题的答案。 I dont know how to use this.我不知道如何使用这个。 any type of help will be useful.任何类型的帮助都会有用。

The issue问题


You are providing the constraints for the views which overlap each other.您正在为彼此重叠的视图提供约束。 This way, they do not get visible.这样,它们就不会被看到。 You must change your layout or constraint the views in different way.您必须更改布局或以不同方式限制视图。 I will give solution for changing the layout.我将提供更改布局的解决方案。

The solution解决方案


Try this code in your xml:在您的 xml 中尝试此代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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:orientation="vertical">
<TextView
    android:id="@+id/text_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="100dp" />
<ToggleButton
    android:id="@+id/toggle_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>
</LinearLayout >

Another issue might be occurring.可能正在发生另一个问题。 Let me explain it here.让我在这里解释一下。

You are only setting the text when the button is checked.您仅在选中按钮时设置文本。 Not when it is not checked.没有检查时不会。 Try to.尝试。 display different text every time it's checked or not.每次检查或不检查时显示不同的文本。 Try this code in second activity:在第二个活动中尝试此代码:

public class MainActivity2 extends AppCompatActivity {
ToggleButton toggleButton;
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    textView=findViewById(R.id.text_view);
toggleButton=findViewById(R.id.toggle_button);
    toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {

                textView.setText("hello MainActivity2");
            } else {
                textView.setText("bye MainActivity2");
            }
        }
    });
}
} 

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

相关问题 如何为不同的活动使用相同的 XML 布局? - How can I use the same XML layout for different activities? 如何在不同的活动上使用GridView的同一适配器 - How Can I use Same Adapter of a GridView on Different activities 我可以在不同的活动中为两个不同的列表视图使用相同的适配器吗? - Can I use the same adapter for two different listviews in different activities? 如何在android中使用相同的XML布局创建多个活动,每个活动都有自己的自定义视图 - How to use same XML layout in android to create several activities, each with their own custom view 我可以在两个活动中使用相同的方法吗? - Can i use the same method in two activities? 我怎样才能同时运行2个活动 - How can i run 2 activities at the same time “布局”是否与 Android Studio 中的“布局资源”相同? 如何以编程方式创建 XML 布局资源? - Is "layout" the same as "layout resource" in Android Studio? How can I programmingly create an XML layout resource? 如何在不同的活动(Android和Java)中使用相同的ArrayList - How to use the same ArrayList in different activities (Android and Java) 如何在不复制代码的情况下在不同的活动上做同样的事情 - how can make the same thing on different activities without copying code 如何在不使用活动的情况下创建其他页面 - How can i create different pages without using activities
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM