简体   繁体   English

如何在Android中创建具有均等大小的列的项目列表

[英]How do I create a list of items with equally sized columns in Android

I want to achieve something like this in Android: 我想在Android中实现以下目标:

表

Note that the content is dynamic, so the width of the columns cannot be fixed at compile time. 请注意,内容是动态的,因此在编译时无法固定列的宽度。 The number of rows is dynamic, however the number of columns is fixed. 行数是动态的,但是列数是固定的。

In WPF I would use an ItemsControl with a StackPanel as the ItemsPanel . 在WPF中,我将使用带有StackPanelItemsControl作为ItemsPanel I would then set the ItemTemplate to a DataTemplate containing a Grid with ColumnDefinition s and SharedSizeGroup s. 然后,我将ItemTemplate设置为一个DataTemplate其中包含一个带有ColumnDefinitionSharedSizeGroupGrid Something like this: 像这样:

<DataTemplate DataType="MyViewModel" x:Key="MyItemTemplate">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition SharedSizeGroup="Col0"/>
            <ColumnDefinition SharedSizeGroup="Col1"/>
            <ColumnDefinition SharedSizeGroup="Col2"/>
        </Grid.ColumnDefinitions>
        <TextBlock Grid.Column="0" Text={Binding Column0} Margin="0 0 5 0"/>
        <TextBlock Grid.Column="1" Text={Binding Column1} Margin="0 0 5 0"/>
        <TextBlock Grid.Column="2" Text={Binding Column2}/>
    </Grid>
</DataTemplate>

<ItemsControl ItemTemplate={StaticResource MyItemTemplate}>
    <ItemsControl.ItemsPanel>
        <StackPanel Grid.IsSharedSizeScope="True"/>
    </ItemsControl>
</ItemsControl>

I cannot figure out how to do it in Android. 我不知道如何在Android中做到这一点。 I know I have to use a RecyclerView (the equivalent of an ItemsControl ) and a LinearLayoutManager (the equivalent of the StackPanel ). 我知道我必须使用RecyclerView (相当于ItemsControl )和LinearLayoutManager (相当于StackPanel )。 But how do I match up the column sizes in each item? 但是如何匹配每个项目中的列大小?

You are supposed to use RecyclerView . 您应该使用RecyclerView But as a first step, try with GridView : 但首先,请尝试使用GridView

<GridView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:stretchMode="columnWidth"
    android:numColumns="3"
    android:verticalSpacing="5dp"
    android:horizontalSpacing="5dp"
    android:padding="10dp"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何重叠三个相等大小的视图,并且它们之间的视图较小? - How do I overlap three equally-sized views with smaller views between them? 如何比较 Android 列表中的项目? - How do I compare items in list in Android? 如何使用 Android Studio 创建包含动态项目数的列表 - How can I create List with dynamically number of items with Android Studio 如何将jsonObject表示为android中的列表项? - How do I represent a jsonObject as list items in android? 如何在 Xamarin Picker 中设置项目列表的样式(在 Android 中) - How do I style the items list in a Xamarin Picker (in Android) 如何在Android中以编程方式创建xml项目列表? - How to create xml items list programmatically in Android? 如何在Android Javascript中创建动态大小的TextArea - How To Create A Dynamically Sized TextArea In Android Javascript 如何在 Android 中创建下拉复选框列表? - How do I create a dropdown checkbox list in Android? Android-如何在类似HTML的Ordered列表中显示项目列表? - Android - how do I display a list of items in an HTML-like Ordered list? 以编程方式平均分割列表片段中的项目 - Programatically splitting items in a list fragment equally
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM