为什么部分文字位于屏幕后面?

时间:2019-04-21 14:06:09

标签: android xml textview

我遇到以下问题:textview中的部分文本位于屏幕后面

enter image description here

我不明白为什么通常在我使用class PetrolStationListActivity : AppCompatActivity() { private lateinit var fusedLocationProviderClient: FusedLocationProviderClient private lateinit var listView: RecyclerView private var currentLocation: Location? = null override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) { //some code } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.petrol_station_list_view) requestLocationPermission() fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this) getLastLocation() initPetrolStationList() } fun addPetrolStation(v: View) { if (currentLocation != null) { val googleMapController = GoogleMapsController() googleMapController.getPetrolStationFromGoogle(object : GoogleResponse { override fun getLocation(): Location = currentLocation!! override fun getResponse(body: GoogleMapResponse) { if (body.status == "OK") { val intent = Intent(applicationContext, PetrolStationActivity::class.java) v.context.startActivity(intent) } else { Toast.makeText(applicationContext, "Petrol station not found", Toast.LENGTH_LONG).show() } } }) } } @SuppressLint("MissingPermission") private fun getLastLocation() { fusedLocationProviderClient.lastLocation.addOnCompleteListener { if (it.isSuccessful) { currentLocation = it.result!! Log.d("Current location: ", "Lat:${currentLocation!!.latitude}, Lng:${currentLocation!!.longitude}") } else { Log.d("Location exception: ", it.exception?.message) Toast.makeText(this, "Location not found :( ", Toast.LENGTH_LONG).show() } } } private fun initPetrolStationList() { if (currentLocation != null) { val petrolStationController = PetrolStationController() //some code } else Toast.makeText(this, "Location not found", Toast.LENGTH_LONG).show() } } 时会发生这种情况,所以文本有几行并且所有行都在屏幕上。我向所有textview添加了wrap_content,但是仍然发生相同的问题。这是具有所有这些textviews的xml文件:

android:layout_marginEnd="16dp"

那么,为什么会发生此问题,我该如何解决呢?

UPD

当我将<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.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=".FilmDetailActivity"> <ImageView android:id="@+id/avatar_imageview" android:layout_width="150dp" android:layout_height="250dp" android:layout_marginStart="8dp" android:layout_marginTop="8dp" android:layout_marginEnd="16dp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" tools:ignore="ContentDescription" /> <TextView android:id="@+id/title_textview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="8dp" android:layout_marginStart="16dp" android:layout_marginEnd="16dp" app:layout_constraintStart_toEndOf="@id/avatar_imageview" app:layout_constraintTop_toTopOf="parent"/> <TextView android:id="@+id/year_textview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="16dp" android:layout_marginStart="16dp" android:layout_marginEnd="16dp" android:text="@string/year" app:layout_constraintStart_toEndOf="@id/avatar_imageview" app:layout_constraintTop_toBottomOf="@id/title_textview"/> <TextView android:id="@+id/runtime_textview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="16dp" android:layout_marginStart="16dp" android:layout_marginEnd="16dp" android:text="@string/runtime" app:layout_constraintStart_toEndOf="@id/avatar_imageview" app:layout_constraintTop_toBottomOf="@id/year_textview"/> <TextView android:id="@+id/director_textview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="16dp" android:layout_marginStart="16dp" android:layout_marginEnd="16dp" android:text="@string/director" app:layout_constraintStart_toEndOf="@id/avatar_imageview" app:layout_constraintTop_toBottomOf="@id/runtime_textview"/> <TextView android:id="@+id/actors_textview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="16dp" android:layout_marginStart="16dp" android:layout_marginEnd="16dp" android:text="@string/actors" app:layout_constraintStart_toEndOf="@id/avatar_imageview" app:layout_constraintTop_toBottomOf="@id/director_textview"/> <TextView android:id="@+id/plot_textview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="16dp" android:layout_marginStart="16dp" android:layout_marginEnd="16dp" android:text="@string/plot" app:layout_constraintStart_toEndOf="@id/avatar_imageview" app:layout_constraintTop_toBottomOf="@id/actors_textview" /> <TextView android:id="@+id/language_textview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="16dp" android:layout_marginStart="16dp" android:layout_marginEnd="16dp" android:text="@string/language" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/plot_textview" /> <TextView android:id="@+id/country_textview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="16dp" android:layout_marginTop="16dp" android:layout_marginEnd="16dp" android:text="@string/country" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/language_textview" /> <TextView android:id="@+id/imdb_rating_textview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="16dp" android:layout_marginStart="16dp" android:layout_marginEnd="16dp" android:text="@string/imdb_rating" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/country_textview"/> </android.support.constraint.ConstraintLayout> 添加到每个app:layout_constraintEnd_toEndOf="parent"时,我面临以下问题:

enter image description here

UPD 2

当我添加TextView时,我发现了相同的问题,

enter image description here

2 个答案:

答案 0 :(得分:2)

在文本视图中将layout_width="wrap_content"更改为layout_width="0dp"

app:layout_constraintEnd_toEndOf="parent"添加到您的TextView中以确保它们适合布局

此外,如果您的文本居中对齐,请添加app:layout_constraintHorizontal_bias="0"使其与左侧对齐(“ 1”代表右对齐)

答案 1 :(得分:1)

app:layout_constraintEnd_toEndOf="parent"(右约束)添加到TextView并将layout_width设置为0dp

这是您的布局的更新版本


<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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">

    <ImageView
        android:id="@+id/avatar_imageview"
        android:layout_width="150dp"
        android:layout_height="250dp"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="16dp"
        android:layout_marginRight="8dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:ignore="ContentDescription" />

    <TextView
        android:id="@+id/title_textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="16dp"
        app:layout_constraintStart_toEndOf="@id/avatar_imageview"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/year_textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="16dp"
        android:text="2017"
        app:layout_constraintStart_toEndOf="@id/avatar_imageview"
        app:layout_constraintTop_toBottomOf="@id/title_textview" />

    <TextView
        android:id="@+id/runtime_textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="16dp"
        android:text="2 hours 1 minute"
        app:layout_constraintStart_toEndOf="@id/avatar_imageview"
        app:layout_constraintTop_toBottomOf="@id/year_textview" />

    <TextView
        android:id="@+id/director_textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="16dp"
        android:text="Mr James Collon"
        app:layout_constraintStart_toEndOf="@id/avatar_imageview"
        app:layout_constraintTop_toBottomOf="@id/runtime_textview" />

    <TextView
        android:id="@+id/actors_textview"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="16dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@id/avatar_imageview"
        app:layout_constraintTop_toBottomOf="@id/director_textview" />

    <TextView
        android:id="@+id/plot_textview"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="16dp"
        android:text="Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged."
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@id/avatar_imageview"
        app:layout_constraintTop_toBottomOf="@id/actors_textview" />

    <TextView
        android:id="@+id/language_textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="16dp"
        android:text="English"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/plot_textview" />

    <TextView
        android:id="@+id/country_textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="16dp"
        android:text="India"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/language_textview" />

    <TextView
        android:id="@+id/imdb_rating_textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="16dp"
        android:text="7/10"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/country_textview" />

</android.support.constraint.ConstraintLayout>

注意:出于演示目的,仅对plot文本视图进行了修复。请对其他文本视图也进行同样的操作。