繁体   English   中英

LinearLayout 中每行一个 TextView

[英]One TextView per line in LinearLayout

我目前正在启动一个 Android 应用程序,并希望构建一个列表,其中每个项目占据一行。 现在我只有一个 TextView 但以后会有更多的元素。

我在每行放置一个元素时遇到问题,因为 TextView 只是一个接一个地放置,如下所示:

Android 查看

布局如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@+id/twitterNumber"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="@dimen/text_margin"
        android:textAppearance="?attr/textAppearanceListItem" />

</LinearLayout>

RecyclerView 的代码是:

<?xml version="1.0" encoding="utf-8"?>
<androidx.recyclerview.widget.RecyclerView 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:id="@+id/list"
    android:name="codehero.twitteralarmclock.ui.main.tweet.TweetsFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    app:layoutManager="LinearLayoutManager"
    tools:context=".ui.main.tweet.TweetsFragment"
    tools:listitem="@layout/fragment_tweets" />

我尝试更改 LinearLayout 的方向,将 maxLines 和 singleLine 属性添加到 TextView 但没有任何改变。

提前感谢您的帮助!

将线性布局更改为约束布局,然后重新运行代码并检查

<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="wrap_content">


<TextView
        android:id="@+id/twitterNumber"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/text_margin"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" 
        android:textAppearance="?attr/textAppearanceListItem" />


</androidx.constraintlayout.widget.ConstraintLayout>



如果这仍然不能解决您的问题,请尝试摆脱文本外观属性和布局边距属性并重新编译。

如果它有效,那么您就知道这是属性的问题

如果仍然无法解决,您可能需要粘贴您的适配器代码

因此,如果列表计数大于 1,Android Studio 默认 FragmentList 代码将创建一个 GridLayout:

class TweetsFragment : Fragment() {

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view = inflater.inflate(R.layout.fragment_tweets_list, container, false)

        // Set the adapter
        if (view is RecyclerView) {
            with(view) {
                layoutManager = when {
                    columnCount <= 1 -> LinearLayoutManager(context)
                    else -> GridLayoutManager(context, columnCount)
                }
                adapter = TweetsViewAdapter(TwitterPlaceholderContent.ITEMS)
            }
        }
        return view
    }
}

解决方案是始终在 onCreateView 方法中创建一个 LinearLayout:

if (view is RecyclerView) {
    with(view) {
        layoutManager = LinearLayoutManager(context)
        adapter = TweetsViewAdapter(TwitterPlaceholderContent.ITEMS)
    }
}

暂无
暂无

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

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