[英]One TextView per line in LinearLayout
I am currently starting an Android app and want to build a list where each item occupies one single row.我目前正在启动一个 Android 应用程序,并希望构建一个列表,其中每个项目占据一行。 Right now I only have a TextView but will have more elements later.
现在我只有一个 TextView 但以后会有更多的元素。
I am having problems in placing one element per row since the TextViews just get placed one after the other, like this:我在每行放置一个元素时遇到问题,因为 TextView 只是一个接一个地放置,如下所示:
The layout for this is as follows:布局如下:
<?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>
The code for the RecyclerView is: 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" />
I've tried changing the orientation of the LinearLayout, adding the maxLines and singleLine attributes to the TextView but nothing changed.我尝试更改 LinearLayout 的方向,将 maxLines 和 singleLine 属性添加到 TextView 但没有任何改变。
Thanks for you help in advance!提前感谢您的帮助!
Change the linear layout to a constraint layout like so and re run the code and check将线性布局更改为约束布局,然后重新运行代码并检查
<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>
If this still doesn't solve your issue try to get rid of the text appearance attribute and layout margin attribute and recompile.如果这仍然不能解决您的问题,请尝试摆脱文本外观属性和布局边距属性并重新编译。
If it works then you know it's a problem of the attributes如果它有效,那么您就知道这是属性的问题
If it still does not solve you might have to paste your adapter code如果仍然无法解决,您可能需要粘贴您的适配器代码
So it turns the Android Studio default FragmentList code will create a GridLayout if the list count is bigger than 1:因此,如果列表计数大于 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
}
}
The solution is to always create a LinearLayout inside the onCreateView method:解决方案是始终在 onCreateView 方法中创建一个 LinearLayout:
if (view is RecyclerView) {
with(view) {
layoutManager = LinearLayoutManager(context)
adapter = TweetsViewAdapter(TwitterPlaceholderContent.ITEMS)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.