繁体   English   中英

将多个文本视图添加到水平线性布局

[英]Adding multiple textviews to horizontal linear layout

嗨,我运行了 10 次循环,每次我创建一个新的 TextView 时,我都会将该文本视图添加到我的线性布局中,该布局的方向设置为“HORIZONTAL”。 我想以段落的形式显示这些文本,其中另一个句子从一个句子结束。

这是我的布局:

<ScrollView 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_height="match_parent"
android:layout_width="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".ui.QuestionActivity"
    android:padding="16dp"
    android:orientation="vertical"
    tools:showIn="@layout/activity_question">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/title"
        android:layout_gravity="center"
        android:textAppearance="?android:textAppearanceLarge"
        android:textColor="@android:color/black"
        android:textStyle="bold|italic"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:layout_marginTop="10dp"
        android:id="@+id/base_layout"/>

</LinearLayout>

</ScrollView>

这是我的Java代码:

for(int i=0; i<10; i++){
        TextView textView = Utility.getTextView(this, i);

        if(i == 5) {
            textView.setText(sentences.get(i) + "\n" + "\n");
        }
        else {
            textView.setText(sentences.get(i) + " ");
        }

        mLayout.addView(textView);
    }

但问题是当方向设置为“水平”时只显示第一个文本视图,如果我将我的方向设置为“垂直”,我可以看到彼此下方的所有文本视图。

请帮我解决这个问题。

您应该需要将 base_layout LinearLayout包装在HorizontalScrollView下。

试试下面,应该可以解决你的问题。 我已经验证过自己。

<HorizontalScrollView
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:scrollbars="horizontal">

      <LinearLayout
           android:id="@+id/base_layout"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:layout_marginTop="10dp"
           android:orientation="horizontal" />
</HorizontalScrollView>

暂无
暂无

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

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