【问题标题】:A way to Wrap LinearLayout一种包装 LinearLayout 的方法
【发布时间】:2018-06-09 22:35:28
【问题描述】:

几周以来我一直在努力寻找解决方案,并一直将其列入我的后备名单,我无法包装 LinearLayout 的高度。

我有一个简单的线性布局

<LinearLayout
android:id="@+id/tagsLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="left"
android:paddingRight="10dp"
android:paddingBottom="5dp"
android:padding="5dp"/>

然后我使用我的代码将多个TextViews 添加到带有标签链接样式的LinearLayout

Txt.SetBackgroundResource(Resource.Drawable.roundtext);
Txt.SetPadding(30, 10, 30, 10);
Txt.SetTextColor(global::Android.Graphics.Color.ParseColor("#373944"));
Txt.SetTextSize(Android.Util.ComplexUnitType.Dip, 12);

结果如下

看到最后一个TextView永远不会进入下一行

我查找了很多线程和解决方案,结果发现LinearLayout 不能像这样包装内容相反,我将不得不创建自己的布局,例如FlowLayout

这个问题有什么简单的解决办法吗?

【问题讨论】:

    标签: android xamarin


    【解决方案1】:

    您可以使用GridView 代替LinearLayout

    <GridView
        android:id="@+id/tagsLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:numColumns="auto_fit"
        android:horizontalSpacing="5dp"
        android:verticalSpacing="5dp"/>
    

    这应该会自动将TextView 放到下一行。如果你想要更动态的东西,那么你可以使用FlowLayout 库或StaggeredLayout。查看这个SO,它声称它可以替代 FlowLayout。

    【讨论】:

    • 嗨,感谢您的回复,听起来是个不错的解决方案,但是当我添加 TextViews 时,例如 myGridView.AddView(TEXTVIEW) 我收到错误 Java.Lang.UnsupportedOperationException 似乎不支持 AddView
    • 使用 ArrayAdapter 将项目添加到 GridView 但它没有给出想要的结果,它只是将文本框划分为列。
    • @aliusman 使用此SO 中所示的自定义适配器或使用此blog 中所示默认ID 的数组适配器
    • @aliusman 查看此SO for Xamarin
    • 我使用了ArrayAdapter,它有效(排序)它实际上将文本分成列,我正在寻找这样的解决方案i.stack.imgur.com/yFf2W.png
    【解决方案2】:

    看看flexbox-layout。 Gradle 易于使用和导入。

    【讨论】:

    • 感谢您的回复,任何与 xamarin 相关的想法,我发现在 C# 中绑定 android 库是如此痛苦
    • 抱歉,没有看到您需要 Xamarin 解决方案,很遗憾我没有得到答案。
    • 我投了赞成票,因为我发现已经有一个可用的 c# 绑定。感谢您的建议
    猜你喜欢
    • 1970-01-01
    • 2011-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-30
    • 1970-01-01
    相关资源
    最近更新 更多