【问题标题】:Android: Custom FlowLayoutAndroid:自定义 FlowLayout
【发布时间】:2015-04-12 03:19:35
【问题描述】:

我想在 android 中制作this 类型的标签菜单。动态填充布局并居中对齐。
我该怎么做?

使用@Dory 推荐的库编辑

<FlowLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:f="http://schemas.android.com/apk/res-auto"
    android:orientation="horizontal"
    f:debugDraw="false"
    f:weightDefault="0"
    f:layoutDirection="ltr"
    android:gravity="center"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="6dip"
    android:paddingTop="6dip"
    android:paddingRight="12dip"
    android:paddingBottom="12dip"
    android:background="@android:color/black"
    android:id="@+id/l_flow"/>



   <Button xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_marginBottom="3dp"/>

一个xml文件中的Flowlayout按钮在另一个xml文件中。我正在充气 Button 然后 fLayout.addView(button);

what I get is this视图之间的顶部和底部填充高于预期

【问题讨论】:

标签: android flowlayout


【解决方案1】:

最后我在this 库的帮助下做到了。我只使用了类 FlowLayout 和 attr.xml 我的 main.xml 看起来像这样:

    <com.example.FlowTest.FlowLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/flow"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:background="@android:color/white"/>

我的项目布局如下所示:

<Button xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"  
    android:layout_gravity="center_horizontal"
    android:padding="5dp"/>

在我的 Activity 的 onCreate 方法中:

LayoutInflater mInflater = LayoutInflater.from(this);
    mFlowLayout = (FlowLayout) findViewById(R.id.flow);

    String [] names = {"goods", "shops", "cars", "washing machine","blablabla","clothes","books"};

    for(int i = 0; i<names.length;i++){
        Button b = (Button)mInflater.inflate(R.layout.item_flow,mFlowLayout, false);
        b.setText(names[i]);
        mFlowLayout.addView(b);
    }

【讨论】:

    【解决方案2】:

    看看 here,可以使用this

    编辑:

    Flow Layouthorizontalverticalspacing 中提供了属性。请参阅下面的示例 xml:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    
    xmlns:f="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">   
        <FlowLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        f:horizontalSpacing="5dp"
        f:verticalSpacing="5dp" />  
    </RelativeLayout>       
    

    编码愉快:)

    【讨论】:

    • 那个库帮助了我,但是当我动态添加视图时它失败了。视图之间的空间(垂直)太高,我无法管理它们
    • @user3657472 您能否详细说明您的问题
    • 但在库中没有 f:horizo​​ntalSpacing f:verticalSpacing
    • @user3657472 okie.. 我很久以前就使用过这个库。 Layoutspacing 现在转换为边距。你是动态地还是在 xml 中插入按钮?
    • xml 中的按钮。我在活动中给它充气——> Button b = (Button)mInflater.inflate(R.layout.item_flow,fLayout, false);然后将其添加到 fLayout --->fLayout.addView(b);
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-15
    • 1970-01-01
    • 1970-01-01
    • 2014-06-27
    • 1970-01-01
    • 2019-02-13
    相关资源
    最近更新 更多