簡體   English   中英

Android:間距和TextView包裝問題

[英]Android: spacing and TextView wrapping issue

我想要這樣的布局:

在此處輸入圖片說明

這是我的代碼:

 <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="I. ISI PELATIHAN"/>


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <TextView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:text="1. "
                />

            <TextView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:text=" Kejelasan tujuan dan manfaat pelatihan"
               />

            <RatingBar
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                style="?android:attr/ratingBarStyleSmall"
                android:numStars="5"
                android:stepSize="1"
                android:layout_gravity="right"/>
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <TextView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:text="2. "
                />

            <TextView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:text=" Penampilan bahan/materi pelatihan"
                />

            <RatingBar
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                style="?android:attr/ratingBarStyleSmall"
                android:numStars="5"
                android:stepSize="1"
                android:layout_gravity="right"/>
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <TextView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:text="3. "
                />

            <TextView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:text=" Kelengkapan bahan/materi pilihan"
                />

            <RatingBar
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                style="?android:attr/ratingBarStyleSmall"
                android:numStars="5"
                android:stepSize="1"
                android:layout_gravity="right"/>
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <TextView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:text="4. "
                />

            <TextView
                android:layout_width="wrap_content"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:text=" Metode yang digunakan membantu pemahaman isi program"
                />

            <RatingBar
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                style="?android:attr/ratingBarStyleSmall"
                android:numStars="5"
                android:stepSize="1"
                android:layout_gravity="right"/>
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <TextView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:text="5. "
                />

            <TextView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:text=" Saya mengerti/memahami program/materi yang diberikan"
                />

            <RatingBar
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                style="?android:attr/ratingBarStyleSmall"
                android:numStars="5"
                android:stepSize="1"
                android:layout_gravity="right"/>
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <TextView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:text="6. "
                />

            <TextView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:text=" Saya belajar sesuatu yang baru dari materi yang diberikan"
                />

            <RatingBar
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                style="?android:attr/ratingBarStyleSmall"
                android:numStars="5"
                android:stepSize="1"
                android:layout_gravity="right"/>
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <TextView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:text="7. "
                />

            <TextView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:text=" Efisiensi penggunaan waktu"
                />

            <RatingBar
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                style="?android:attr/ratingBarStyleSmall"
                android:numStars="5"
                android:stepSize="1"
                android:layout_gravity="right"/>
        </LinearLayout>


    </LinearLayout>

結果是:

在此處輸入圖片說明

我想知道的是:

  1. 如何在行號之前和RatingBar之后添加填充?
  2. 我在文本上使用wrap_content 對於較短的文本似乎可以,但是對於較長的文本,它是混亂的。 如何設置所有文本使用相同的長度,並且如果文本長度超過某個值,它將像第一個圖片所示那樣正確包裝?

在guildLine的幫助下使用約束布局和單獨的評級欄

您可以在一種布局中設置所有視圖。

使用最新的Beta版依賴性

編譯'com.android.support.constraint:constraint-layout:1.1.0-beta5'

這里示例教程

https://constraintlayout.com/basics/

官方教程

https://developer.android.com/reference/android/support/constraint/ConstraintLayout.html

這是帶有guideLine的ConstraintLayout的示例代碼

<?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"
android:padding="16dp"
tools:context="android.itomerbu.layoutdemo.MainActivity">

<android.support.constraint.Guideline
    android:id="@+id/guideline"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    app:layout_constraintGuide_percent="0.44"/>

<TextView
    android:id="@+id/btnThird"
    android:layout_width="0dp"
    app:layout_constraintHorizontal_weight="1"
    android:layout_height="wrap_content"
    android:text="@string/btnThird"
    app:layout_constraintLeft_toLeftOf="parent"
    android:layout_marginBottom="8dp"
    app:layout_constraintRight_toLeftOf="@+id/btnTwoThirds"
    app:layout_constraintBottom_toTopOf="@+id/guideline"
    android:layout_marginStart="8dp"
    android:layout_marginLeft="8dp"/>

<RatingBar
    android:id="@+id/btnTwoThirds"
    app:layout_constraintHorizontal_weight="2"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:text="@string/btnTwoThirds"
    app:layout_constraintBottom_toBottomOf="@+id/btnThird"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintLeft_toRightOf="@+id/btnThird"/>
  </android.support.constraint.ConstraintLayout>

您可以使用RelativeLayout實現所需的功能。 將此xml布局示例插入xml布局文件中。 您可以根據需要更改id ,並根據需要使用填充和邊距。 添加盡可能多RelativeLayout根據您的需要為您團體Activity

如果您有任何問題或疑問,請告訴我。

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="5dp"
        >
        <TextView
            android:id="@+id/tvFirst"
            android:layout_width="25dp"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:paddingEnd="4dp"
            android:paddingStart="4dp"
            android:text="1"
            android:textAlignment="center"/>
        <TextView
            android:id="@+id/tvFirstTitle"
            android:text="@string/SomeTextString"
            android:layout_toEndOf="@id/tvFirst"
            android:layout_toStartOf="@id/rBarFirst"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            />

        <RatingBar
            android:id="@+id/rBarFirst"
            style="@style/Widget.AppCompat.RatingBar.Small"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:numStars="5"
            android:stepSize="1"
            android:layout_marginEnd="10dp"
            />
    </RelativeLayout>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="5dp"
        >
        <TextView
            android:id="@+id/tvSecond"
            android:layout_width="25dp"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:paddingEnd="4dp"
            android:paddingStart="4dp"
            android:text="2"
            android:textAlignment="center"/>
        <TextView
            android:id="@+id/tvSecondTitle"
            android:text="@string/SomeTextString"
            android:layout_toEndOf="@id/tvSecond"
            android:layout_toStartOf="@id/rBarSecond"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            />

        <RatingBar
            android:id="@+id/rBarSecond"
            style="@style/Widget.AppCompat.RatingBar.Small"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:numStars="5"
            android:stepSize="1"
            android:layout_marginEnd="10dp"
            />
    </RelativeLayout>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="5dp"
        >
        <TextView
            android:id="@+id/tvThird"
            android:layout_width="25dp"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:paddingEnd="4dp"
            android:paddingStart="4dp"
            android:text="3"
            android:textAlignment="center"/>
        <TextView
            android:id="@+id/tvThirdTitle"
            android:text="@string/SomeTextString"
            android:layout_toEndOf="@id/tvThird"
            android:layout_toStartOf="@id/rBarThird"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            />

        <RatingBar
            android:id="@+id/rBarThird"
            style="@style/Widget.AppCompat.RatingBar.Small"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:numStars="5"
            android:stepSize="1"
            android:layout_marginEnd="10dp"
            />
    </RelativeLayout>
</LinearLayout>

請注意:

我只是注意到您正在使用: style="?android:attr/ratingBarStyleSmall"您可能需要更改示例中使用的RatingBarstyle ,以與您的應用程序兼容!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM