【问题标题】:Adding multiple textviews to horizontal linear layout将多个文本视图添加到水平线性布局
【发布时间】:2018-08-18 15:47:59
【问题描述】:

嗨,我正在运行一个循环 10 次,每次我创建一个新的 TextView 并且我将该 textview 添加到我的线性布局中,其方向设置为“水平”。我想以段落的形式显示这些文本,另一个句子开始于一个句子结束的地方。

这是我的布局:

<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);
    }

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

请帮帮我。

【问题讨论】:

  • 您是否尝试在语法上设置方向?
  • 不工作尝试谢谢

标签: java android android-layout android-linearlayout textview


【解决方案1】:

您应该需要将 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>

【讨论】:

  • 谢谢@Krishna Sharma 但这意味着我的文本视图将是我不想要的水平滚动,我想以段落的形式显示它们
  • 那么所有的文本视图都不会在屏幕上水平显示。这就是你的情况。
  • 那么我应该如何解决这个问题?我可以知道他们没有显示的原因吗
  • 只有选项是方向垂直。如果你想要水平,那么你必须将你的布局包裹在水平滚动视图下。
  • 如果您从布局中删除 ScrollView,那么即使是垂直方向,所有文本视图也不会显示。无论如何,无论是垂直还是水平,您都需要滚动。
猜你喜欢
  • 2023-03-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-28
  • 1970-01-01
  • 1970-01-01
  • 2016-10-27
相关资源
最近更新 更多