布局如何水平和垂直适应内容?
How the layout adapt to content horizontally and vertically?
有什么方法可以创建水平 wrap_content 的布局,如果该行已满,则将其余部分放在新行中。
示例:
如果我创建一个方向设置为水平的线性布局,然后我创建三个按钮填充屏幕大小(按钮)(按钮)(按钮),现在如果我添加更多按钮,它将以非常难看的方式放置在该行的末尾。我想要的是将新按钮放置在新行中,并且我不想创建多个布局,因为这些按钮是根据 API 输入从 java 动态放置的。有没有办法做到这一点,或者我只需要设计一个函数来处理它?
好吧,如果您的按钮具有不同的宽度大小,那么您可以使用 GridView
,您将其 android:numColumns
设置为 auto_fit
,如果第一行已经填满按钮,则下一次您添加的按钮将被添加到下一个 line/row 的网格中。
假设您有一个宽度为 match_parent
的按钮,那么添加的下一个按钮将在下一行,因此如果您有一个 fixed 按钮width 然后它将添加彼此相邻的按钮,直到它填满 gridview 宽度,下一个将添加的按钮将在下一行。
如果你想实现gridview那么你可以去here.
思考GridView
,参考Android Developer
您应该在 your_layout.xml
中定义网格视图容器
<your.package.ButtonsGridView
android:id="@+id/buttonsGridView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="auto_fit"
android:stretchMode="columnWidth" />
然后将 GridView
扩展到名为 ButtonsGridView
、
的自定义 class
然后将 ArrayAdapter<T>
扩展到名为 ButtonsAdapter
的自定义 class,再到管理器数据。
并将ButtonsAdapter
设置为ButtonsGridView
ButtonsGridView buttonsGridView = (ButtonsGridView) findViewById(R.id.buttonsGridView);
ButtonsAdapter buttonsAdapter = new ButtonsAdapter(Context);
buttonsGridView.setAdapter(buttonsAdapter);
考虑到不同的按钮宽度,你可以在适配器的方法中管理宽度public View getView(int position, View convertView, ViewGroup parent)
,你可以做任何你想自定义按钮的事情。
参考这个问题和答案Grid View change column number runtime?更改列号。
如果您的 View
是动态添加的,也许您想使用 GridView
because it use an Adapter
to obtain the View
s that will be displayed on screen (same as ListView
). But if you will add View
s to the Layout in Design time, maybe you can use GridLayout
Xamarin 有很好的解释 GridLayout
检查一下 here
您还可以在 SO 中检查 this question 以查看 GridView
和 GridLayout
之间更详细的区别:
有什么方法可以创建水平 wrap_content 的布局,如果该行已满,则将其余部分放在新行中。
示例:
如果我创建一个方向设置为水平的线性布局,然后我创建三个按钮填充屏幕大小(按钮)(按钮)(按钮),现在如果我添加更多按钮,它将以非常难看的方式放置在该行的末尾。我想要的是将新按钮放置在新行中,并且我不想创建多个布局,因为这些按钮是根据 API 输入从 java 动态放置的。有没有办法做到这一点,或者我只需要设计一个函数来处理它?
好吧,如果您的按钮具有不同的宽度大小,那么您可以使用 GridView
,您将其 android:numColumns
设置为 auto_fit
,如果第一行已经填满按钮,则下一次您添加的按钮将被添加到下一个 line/row 的网格中。
假设您有一个宽度为 match_parent
的按钮,那么添加的下一个按钮将在下一行,因此如果您有一个 fixed 按钮width 然后它将添加彼此相邻的按钮,直到它填满 gridview 宽度,下一个将添加的按钮将在下一行。
如果你想实现gridview那么你可以去here.
思考GridView
,参考Android Developer
您应该在 your_layout.xml
中定义网格视图容器<your.package.ButtonsGridView
android:id="@+id/buttonsGridView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="auto_fit"
android:stretchMode="columnWidth" />
然后将 GridView
扩展到名为 ButtonsGridView
、
然后将 ArrayAdapter<T>
扩展到名为 ButtonsAdapter
的自定义 class,再到管理器数据。
并将ButtonsAdapter
设置为ButtonsGridView
ButtonsGridView buttonsGridView = (ButtonsGridView) findViewById(R.id.buttonsGridView);
ButtonsAdapter buttonsAdapter = new ButtonsAdapter(Context);
buttonsGridView.setAdapter(buttonsAdapter);
考虑到不同的按钮宽度,你可以在适配器的方法中管理宽度public View getView(int position, View convertView, ViewGroup parent)
,你可以做任何你想自定义按钮的事情。
参考这个问题和答案Grid View change column number runtime?更改列号。
如果您的 View
是动态添加的,也许您想使用 GridView
because it use an Adapter
to obtain the View
s that will be displayed on screen (same as ListView
). But if you will add View
s to the Layout in Design time, maybe you can use GridLayout
Xamarin 有很好的解释 GridLayout
检查一下 here
您还可以在 SO 中检查 this question 以查看 GridView
和 GridLayout
之间更详细的区别: