我的 java 和 xml 代碼有問題。當我使用 Android Studio IDE 時,我可以看到我想要制作的應用頁面的好版本,而當我將它匯出到我的手機并構建它時,它無法正常作業,一切都在彼此之上。我將展示我在 ide 中看到的代碼和圖片以及我在手機上看到的內容。我希望你能幫助解決這個問題,因為我是一個初學者 android 開發人員,因為我主要在 web 上,這個問題真的很困擾我。謝謝! activity_main.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"
android:background="#fffa94"
tools:context=".Definition">
<TextView
android:id="@ id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="88dp"
android:text="What About Anemia?"
android:textSize="25dp"
app:layout_constraintEnd_toEndOf="parent"
tools:layout_editor_absoluteY="128dp" />
<ImageView
android:id="@ id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/secondpage"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="128dp" />
<TextView
android:id="@ id/textView2"
android:layout_width="347dp"
android:layout_height="189dp"
android:text="Anemia is defined as a low number of red blood cells. In a routine blood test, anemia is reported as a low hemoglobin or hematocrit. Hemoglobin is the main protein in your red blood cells. It carries oxygen, and delivers it throughout your body. If you have anemia, your hemoglobin level will be low too. If it is low enough, your tissues or organs may not get enough oxygen."
android:textAlignment="center"
tools:layout_editor_absoluteX="32dp"
tools:layout_editor_absoluteY="382dp" />
<TextView
android:id="@ id/textView3"
android:layout_width="248dp"
android:layout_height="56dp"
android:text="HOME"
android:textAlignment="center"
android:textSize="40dp"
tools:layout_editor_absoluteX="81dp"
tools:layout_editor_absoluteY="27dp" />
<TextView
android:id="@ id/textView4"
android:layout_width="401dp"
android:layout_height="50dp"
android:text="What are the symptoms?"
android:textAlignment="center"
android:textSize="25dp"
tools:layout_editor_absoluteX="5dp"
tools:layout_editor_absoluteY="571dp" />
<TextView
android:id="@ id/textView5"
android:layout_width="413dp"
android:layout_height="197dp"
android:text="Fatigue. \n Weakness. \n Pale or yellowish skin. \n Irregular heartbeats. \n Shortness of breath. \n Diziness or lightheadedness. \n Chest pain. \n Cold hands and feet."
android:textAlignment="center"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="629dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.example.allagainstanemia;
import android.content.Intent;
import android.os.Bundle;
import com.google.android.material.snackbar.Snackbar;
import android.widget.Button;
import androidx.appcompat.widget.Toolbar;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import com.example.allagainstanemia.databinding.ActivityMainBinding;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity {
private AppBarConfiguration appBarConfiguration;
private ActivityMainBinding binding;
private Button button2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
this.button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent otheractivity = new Intent(getApplicationContext(), Definition.class);
startActivity(otheractivity);
finish();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public boolean onSupportNavigateUp() {
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main);
return NavigationUI.navigateUp(navController, appBarConfiguration)
|| super.onSupportNavigateUp();
}
}
我的ide結果圖片

我手機中的應用程式截圖

感謝您的時間!
uj5u.com熱心網友回復:
即使您已將視圖安排在設計的適當位置,它們也沒有約束。您可以在 中的視圖下看到該錯誤activity_main.xml。所以試試這個有一些基本約束的布局。您可以根據需要更改約束
<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"
android:background="#fffa94"
tools:context=".Definition">
<androidx.core.widget.NestedScrollView
android:id="@ id/nestedScrollView3"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@ id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="36dp"
android:text="What About Anemia?"
android:textSize="25dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@ id/textView3" />
<ImageView
android:id="@ id/imageView"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintDimensionRatio="3:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@ id/textView"
app:srcCompat="@drawable/secondpage" />
<TextView
android:id="@ id/textView2"
android:layout_width="347dp"
android:layout_height="189dp"
android:text="Anemia is defined as a low number of red blood cells. In a routine blood test, anemia is reported as a low hemoglobin or hematocrit. Hemoglobin is the main protein in your red blood cells. It carries oxygen, and delivers it throughout your body. If you have anemia, your hemoglobin level will be low too. If it is low enough, your tissues or organs may not get enough oxygen."
android:textAlignment="center"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@ id/imageView" />
<TextView
android:id="@ id/textView3"
android:layout_width="248dp"
android:layout_height="56dp"
android:layout_marginTop="52dp"
android:text="HOME"
android:textAlignment="center"
android:textSize="40dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.496"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@ id/textView4"
android:layout_width="401dp"
android:layout_height="50dp"
android:text="What are the symptoms?"
android:textAlignment="center"
android:textSize="25dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@ id/textView2" />
<TextView
android:id="@ id/textView5"
android:layout_width="413dp"
android:layout_height="197dp"
android:layout_marginTop="69dp"
android:text="Fatigue. \n Weakness. \n Pale or yellowish skin. \n Irregular heartbeats. \n Shortness of breath. \n Diziness or lightheadedness. \n Chest pain. \n Cold hands and feet."
android:textAlignment="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@ id/textView4" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
uj5u.com熱心網友回復:
試試這個我用 UI 做了適當的約束
<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fffa94">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@ id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="HOME"
android:textAlignment="center"
android:textSize="40sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@ id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="What About Anemia?"
android:textSize="25sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@ id/textView3" />
<ImageView
android:id="@ id/imageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:contentDescription="@string/app_name"
android:src="@mipmap/ic_launcher"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@ id/textView" />
<TextView
android:id="@ id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:text="Anemia is defined as a low number of red blood cells. In a routine blood test, anemia is reported as a low hemoglobin or hematocrit. Hemoglobin is the main protein in your red blood cells. It carries oxygen, and delivers it throughout your body. If you have anemia, your hemoglobin level will be low too. If it is low enough, your tissues or organs may not get enough oxygen."
android:textAlignment="center"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@ id/imageView" />
<TextView
android:id="@ id/textView4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:text="What are the symptoms?"
android:textAlignment="center"
android:textSize="25sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@ id/textView2" />
<TextView
android:id="@ id/textView5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:text="Fatigue. \n Weakness. \n Pale or yellowish skin. \n Irregular heartbeats. \n Shortness of breath. \n Diziness or lightheadedness. \n Chest pain. \n Cold hands and feet."
android:textAlignment="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@ id/textView4" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>
uj5u.com熱心網友回復:
您已經使用tools:layout_editor_absoluteX 并且tools:layout_editor_absoluteY 在您的 XML 檔案中,tools:當您在 IDE 中設計 XML 檔案時,該關鍵字用于向視圖添加屬性,并且不適用于構建版本,并且您在使用約束時忘記添加約束布局,希望對你有幫助。
<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"
android:background="#fffa94"
tools:context=".Definition">
<androidx.core.widget.NestedScrollView
android:id="@ id/nestedScrollView3"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@ id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="36dp"
android:text="What About Anemia?"
android:textSize="25dp"
app:layout_constraintTop_toBottomOf="@ id/textView3"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
/>
<ImageView
android:id="@ id/imageView"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintDimensionRatio="3:1"
app:layout_constraintTop_toBottomOf="@ id/textView"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:srcCompat="@drawable/secondpage" />
<TextView
android:id="@ id/textView2"
android:layout_width="347dp"
android:layout_height="189dp"
android:text="Anemia is defined as a low number of red blood cells. In a routine blood test, anemia is reported as a low hemoglobin or hematocrit. Hemoglobin is the main protein in your red blood cells. It carries oxygen, and delivers it throughout your body. If you have anemia, your hemoglobin level will be low too. If it is low enough, your tissues or organs may not get enough oxygen."
android:textAlignment="center"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@ id/imageView"
app:layout_constraintStart_toStartOf="parent"
/>
<TextView
android:id="@ id/textView3"
android:layout_width="248dp"
android:layout_height="56dp"
android:layout_marginTop="52dp"
android:text="HOME"
android:textAlignment="center"
android:textSize="40dp"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@ id/textView4"
android:layout_width="401dp"
android:layout_height="50dp"
android:text="What are the symptoms?"
android:textAlignment="center"
android:textSize="25dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@ id/textView2" />
<TextView
android:id="@ id/textView5"
android:layout_width="413dp"
android:layout_height="197dp"
android:layout_marginTop="69dp"
android:text="Fatigue. \n Weakness. \n Pale or yellowish skin. \n Irregular heartbeats. \n Shortness of breath. \n Diziness or lightheadedness. \n Chest pain. \n Cold hands and feet."
android:textAlignment="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@ id/textView4" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/476539.html
