简体   繁体   English

Android:间距和TextView包装问题

[英]Android: spacing and TextView wrapping issue

I want to have a layout like this: 我想要这样的布局:

在此处输入图片说明

And this is my code: 这是我的代码:

 <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>

which result is: 结果是:

在此处输入图片说明

What I want to know is: 我想知道的是:

  1. How to add padding before the line number and after the RatingBar? 如何在行号之前和RatingBar之后添加填充?
  2. I use wrap_content on the text. 我在文本上使用wrap_content Seems OK for shorter texts, but for longer texts, it's messed up. 对于较短的文本似乎可以,但是对于较长的文本,它是混乱的。 How to set all the texts to use the same length, and if the text length exceed a certain value, it will be wrapped properly like shown by 1st pic? 如何设置所有文本使用相同的长度,并且如果文本长度超过某个值,它将像第一个图片所示那样正确包装?

Use Constraint layout & separate rating bars with help of guildLine 在guildLine的帮助下使用约束布局和单独的评级栏

You can set all views within one layout.. 您可以在一种布局中设置所有视图。

Use latest beta version of dependency 使用最新的Beta版依赖性

compile 'com.android.support.constraint:constraint-layout:1.1.0-beta5' 编译'com.android.support.constraint:constraint-layout:1.1.0-beta5'

Here sample tutorial 这里示例教程

https://constraintlayout.com/basics/ https://constraintlayout.com/basics/

Official tutorial 官方教程

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

Here sample code for ConstraintLayout with guideLine 这是带有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>

You can use RelativeLayout to achieve what you want. 您可以使用RelativeLayout实现所需的功能。 Insert this xml layout example in your xml layout file. 将此xml布局示例插入xml布局文件中。 You can change the id as you need and play around with the padding and margins to fit you needs. 您可以根据需要更改id ,并根据需要使用填充和边距。 Add as many RelativeLayout groups as you need for you Activity . 添加尽可能多RelativeLayout根据您的需要为您团体Activity

Please let me know if you have any issues or questions. 如果您有任何问题或疑问,请告诉我。

<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>

Please Note this: 请注意:

I just noticed that you are using: style="?android:attr/ratingBarStyleSmall" You may need to change the style of the RatingBar I am using in my example to be compatible with your app! 我只是注意到您正在使用: style="?android:attr/ratingBarStyleSmall"您可能需要更改示例中使用的RatingBarstyle ,以与您的应用程序兼容!

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM