【问题标题】:Break the line if there is no space如果没有空格就换行
【发布时间】:2018-04-02 09:18:59
【问题描述】:

如果我在 android 中添加多个文本视图,它会隐藏我需要在屏幕完成时中断 例如,我的代码是这样的

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/layout5"
    android:orientation="horizontal">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is the text"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is the text"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is the text"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is the text"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is the text"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is the text"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is the text"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is the text"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is the text"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is the text"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="last checking text"/>

</LinearLayout>

结果似乎是

我需要在屏幕关闭时分解文本 并换到另一行。 这只是我以编程方式做这件事的示例代码,所以请不要回答添加更多线性布局。

【问题讨论】:

  • 为什么需要太多的textview?
  • 将线性布局的方向更改为垂直
  • 我需要它是水平的但是当屏幕完成时我需要它分解并且下一个文本视图出现在它下面
  • 为什么没有一个TextView 呢?
  • 我正在以编程方式执行此操作,因此我需要多个文本视图,它们将执行不同的操作。我粘贴的代码只是一个例子

标签: android android-layout textview


【解决方案1】:

对于灵活的布局,您可以使用 FlexboxLayout,您可以从 Android Developers Blog 获得详细信息,对于 Open source FlexboxLayout 依赖项,您可以访问 Github。

【讨论】:

    【解决方案2】:
    If you want your textView appearing with multiple line with a scrollable properties, then no need to use multiple textView.
    

    检查以下解决方案:

    <?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="match_parent"`enter code here`
        android:background="#000000"
        android:orientation="vertical" >
    
        <TextView
            android:id="@+id/textView_id"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#40000000"
            android:maxLines="100"
            android:paddingLeft="5dp"
            android:paddingRight="5dp"
            android:scrollbarThumbVertical="@android:color/transparent"
            android:scrollbars="vertical"
            android:textColor="@color/white"
            android:textSize="16sp"
            android:textStyle="bold" />
    
    </LinearLayout>
    
    And bellow code on your Activity:
    TextView txtView = (TextView) findViewById(R.id.TextView_id);
    txtView.setText("your text here");
    txtView.setMovementMethod(new ScrollingMovementMethod());
    

    【讨论】:

      【解决方案3】:

      如果您想以编程方式创建 TextView 并为每个 . 这是代码。首先删除布局中的所有textview。

      TextView tv[] = new TextView[subCategory.length];
          for (int i = 0; i < subCategory.length; i++) {
                  tv[i] = new TextView(this);
                  tv[i].setText(subCategory[i]);
                  tv[i].setId(i);
                  layout5.addView(tv[i]); 
                  tv[i].setOnClickListener(onclicklistener);
              }
      

      对于监听器

      OnClickListener onclicklistener = new OnClickListener() {
      
          @Override
          public void onClick(View v) {
              // TODO Auto-generated method stub
              if(v == tv[0]){
                  //do whatever you want....
              }
          }
      };
      

      【讨论】:

        【解决方案4】:

        如果你想在运行时使用文本视图的数量,你可以尝试使用 GridView as

        <?xml version="1.0" encoding="utf-8"?>
        <GridView xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/gridview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:numColumns="auto_fit"
            android:verticalSpacing="10dp"
            android:horizontalSpacing="10dp"
            android:stretchMode="columnWidth"
            android:gravity="center"
        />
        

        还有 Gridview item as TextView

        <TextView
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:id="@+id/text"
          />
        

        您可以在onItemClickListener()中执行操作//根据位置或textview文本,使用switch case或其他方式选择操作。

           GridView gridView = (GridView) findViewById(R.id.grid_view);
           gridView.setAdapter(yourCustomAdapterObject);
           gridView.setOnItemClickListener(new OnItemClickListener() {
           @Override
           public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
               //here view is your textview
               switch (position) {
                   case 0: //action
                       break;
                   case 1://action
                       break;
                   ....
                   }
               }
          }); 
        

        【讨论】:

          猜你喜欢
          • 2018-02-05
          • 2013-10-10
          • 1970-01-01
          • 1970-01-01
          • 2010-09-26
          • 1970-01-01
          • 2011-03-15
          • 2014-03-14
          • 1970-01-01
          相关资源
          最近更新 更多