【问题标题】:Android dynamic viewsAndroid 动态视图
【发布时间】:2018-08-20 01:51:51
【问题描述】:

我有一个TextView,它在 2 Buttons 旁边显示一条错误消息。它们目前位于水平LinearLayout 内。问题是如果TextView 太宽,2 个Buttons 将被推离屏幕。在这些情况下是否可以将元素向下推?

如果文字很短,没有问题:

(Textview text) (Button1) (Button2)|(Edge of screen) 

如果文本视图很长,我想将 2 个按钮向下按“行”

(Realllllllllllly long text that may|(Edge of screen) 
span 2 lines)

                 (Button1) (Button2)|(Edge of screen) 

【问题讨论】:

  • 你可以通过这个runtime来实现。

标签: android


【解决方案1】:

我认为您需要在水平线性布局下方再保留一个线性布局,如果它的宽度大于所需的两个按钮空间,则需要检查文本大小运行时,然后需要隐藏水平线性布局按钮并需要在布局下方显示按钮

要参考如何检查文本大小运行时请参考以下链接:

Refer this link

【讨论】:

    【解决方案2】:

    试试这个方法:使用FlowLayout

    <org.apmem.tools.layouts.FlowLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
    
    </org.apmem.tools.layouts.FlowLayout>
    

    您可以在 FlowLayout 中放置您的视图,如果不合适,它将自动移至下一行。

    【讨论】:

      【解决方案3】:

      是的,您可以这样做,flexbox-layout 是解决方案。

      如何使用

      Gradle 依赖

      dependencies {
          implementation 'com.google.android:flexbox:0.3.2'
      }
      

      还有xml代码

      <com.google.android.flexbox.FlexboxLayout
          xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:app="http://schemas.android.com/apk/res-auto"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          app:flexWrap="wrap"
          app:alignItems="stretch"
          app:alignContent="stretch" >
      
          <TextView
              android:id="@+id/textview1"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              />
      
          <Button
              android:id="@+id/button1"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              />
      
          <Button
              android:id="@+id/button2"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              app:layout_alignSelf="flex_end"
              />
      </com.google.android.flexbox.FlexboxLayout>
      

      还有一些其他属性[read documentation],您可以尝试并找到更适合您的情况。

      【讨论】:

        【解决方案4】:

        您可以使用 TextView 预定义的方法,像这样对最终用户进行验证

        TextView textView=(TextView)findViewById(R.id.text_view);
        textView.setError("Your Text is very wide please provide short text");
        

        setError 在 textview 视图上加上红色标记,我们可以告诉最终用户。提供的文字很宽

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-02-12
          • 1970-01-01
          • 1970-01-01
          • 2011-09-07
          相关资源
          最近更新 更多