我正在尝试以编程方式使用2列构建GirdLayout,并且我希望这些列的宽度设置为屏幕宽度的一半.
我想你可以从API 21或支持v7 GirdLayout视图执行此操作.我看到使用android:layout_columnWeight ="1"的示例来执行此操作.但我找不到如何以编程方式执行此操作.
任何人都可以帮我解决这个问题吗?
package com.tunify.v3.component;
import java.util.ArrayList;
import android.support.v7.widget.GridLayout;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.ViewGroup;
import com.tunify.asme.data.ChangeableSelectionParameterValue;
public class ChangeableSelectionParameters extends GridLayout{
//private ArrayList<ChangeableSelectionParameterView> selectionParameterViews;
private ArrayList<ChangeableSelectionParameterValue> selectionParameterValues;
private static final int ITEM_PADDING_LEFT_DP = 0;
private static final int ITEM_PADDING_RIGHT_DP = 0;
private static final int ITEM_PADDING_TOP_DP = 0;
private static final int ITEM_PADDING_BOTTOM_DP = 0;
//private static final int MUSICCOLLECTION_DISPLAY_TEXT_SIZE_SP = 15;
public ChangeableSelectionParameters(android.content.Context context, ArrayList<ChangeableSelectionParameterValue> selectionParameterValues) {
super(context);
this.selectionParameterValues = selectionParameterValues;
initializeLayoutBasics(context);
initializeComponents();
}
private …Run Code Online (Sandbox Code Playgroud) 我有一个Android项目,它在大多数菜单和屏幕中使用GridLayout.然而问题是从API 14及以后版本支持GridLayout.
由于我想让应用程序也可用于旧版本的android,我尝试使用Android自己的支持库GridLayout,它增加了对API 7的支持.这正是我所寻找的,但是我不能为我的生活让它工作.我尝试了所有这些解释和想法:
和更多...
无论我做什么,我是怎么做的,或者我使用的是什么IDE(无论是Eclipse ADT还是Android Studio),它总是在布局XML中给出一个错误:
The following classes could be instantiated: - android.support.v7.widget.GridLayout
With either one of these exceptions showing in the error log:
1. android.content.res.Resources$NotFoundException: Could not resolve value 0x7F080000
2. java.lang.classnotfoundexception: android.support.v7.gridlayout.R$dimen
Run Code Online (Sandbox Code Playgroud)
编辑:作为参考,这是我用来创建支持gridlayout(直接从android示例程序):
<android.support.v7.widget.GridLayout
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="wrap_content"
android:background="@drawable/blue"
android:padding="10dip"
app:columnCount="4"
>
<TextView
android:text="@string/string_test"
/>
<EditText
app:layout_gravity="fill_horizontal"
app:layout_column="0"
app:layout_columnSpan="4"
/>
<Button
android:text="@string/button_test"
app:layout_column="2"
/>
</android.support.v7.widget.GridLayout>
Run Code Online (Sandbox Code Playgroud)
如果上述解决方案都不起作用,我可能做错了什么?有什么我想念的,也许我的原始代码有问题吗?
任何帮助表示赞赏
android grid-layout android-gridlayout android-support-library
是否真的不可能在Android GridLayout中轻松强制相等的列宽?尝试明显的布局......
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="4"
android:rowCount="8"
tools:context=".MainActivity" >
<Button
android:id="@+id/button1"
android:layout_gravity="center"
android:text="Button 1" />
<Button
android:id="@+id/button2"
android:layout_gravity="center"
android:text="Button 2" />
<Button
android:id="@+id/button3"
android:layout_gravity="center"
android:text="Button 3" />
<Button
android:id="@+id/button4"
android:layout_gravity="center"
android:text="Button 4" />
</GridLayout>
Run Code Online (Sandbox Code Playgroud)
...将最右边的按钮放在一个明显比其他三个更宽的列中.(使用按钮文本播放,您可以轻松获得更糟糕的示例.)
我阅读了有关GridLayouts中空间分布过多的Android文档,但看起来很明显甚至列宽也经常(任何可能甚至是典型的)在这样的情况下需要我不得不相信我遗漏了一些明显/容易的东西.
我是ConstraintLayout的新手,我正在尝试使用ConstraintLayout复制GridLayout提供的相同网格行为.
具体来说,我想设计一个两列网格.第一列宽度应尽可能窄,而第二列应占用所有剩余的水平空间.当然,第二列应该位于第一列的右侧,或者更确切地说,位于第一列的最宽视图.
我不知道如何使用ConstraintLayout复制最后一个要求.我不想在两列之间使用网格线,因为第一列不应该具有固定的宽度和百分比宽度,而是应该与其最宽的视图一样宽.
在https://gist.github.com/venator85/499dd82f47b3efbbed7a1e9e1ca1412d我准备了一个布局示例和相应的预览,显示了一个实现我想要的GridLayout.在该布局中的前两个ConstraintLayout尝试显示C1和D1与B1对齐,C2和D2与B2对齐.当B2比A2窄时,A1和C1将重叠.
有帮助吗?
谢谢
android android-layout android-gridlayout android-constraintlayout
我有一个充满按钮的网格布局,现在我希望按钮彼此更远,我应该写什么代码?我试图搜索它,但只找到了GridView的解决方案,而不是GridLayout.
我使用下面的网格布局layout_columnWeight和layout_rowWeight在网格单元格中集中我的视图.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<GridLayout
android:id="@+id/container_grid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="2"
android:rowCount="3"
android:orientation="horizontal">
<View
android:id="@+id/view_red"
android:layout_height="100dp"
android:layout_width="100dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_gravity="center"
android:background="#ff0000"
android:layout_row="0"
android:layout_column="0" />
<View
android:id="@+id/view_green"
android:layout_height="100dp"
android:layout_width="100dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_gravity="center"
android:background="#00ff00"
android:layout_row="0"
android:layout_column="0" />
<View
android:id="@+id/view_blue"
android:layout_height="100dp"
android:layout_width="100dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_gravity="center"
android:background="#0000ff"
android:layout_row="0"
android:layout_column="0" />
</GridLayout>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
但它们仅适用于v21及以上版本.如何在API 21之前提供支持layout_columnWeight和layout_rowWeight功能?
我正在尝试创建一个具有2列的GridLayout,这些列将居中.
我的avtual设计是:
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
custom:rowCount="4"
custom:columnCount="2"
android:orientation="horizontal">
<TimeTableKeeper.Tile
android:layout_width="75dp"
android:layout_height="75dp"
android:gravity="top|left"
android:background="#00FF00"
custom:color="green"
custom:layout_row="0"
custom:layout_column="0" />
<TimeTableKeeper.Tile
android:layout_width="75dp"
android:gravity="top|left"
android:layout_height="75dp"
android:background="#00FF00"
custom:color="blue"
custom:layout_row="0"
custom:layout_column="1" />
</GridLayout>
Run Code Online (Sandbox Code Playgroud)
它看起来像:

我希望这个按钮位于中间,并且它们之间的距离非常合适.
可能吗?
- 编辑:
我也尝试将它放入LinearLayout,没有结果:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<GridLayout xmlns:custom="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
custom:rowCount="4"
custom:columnCount="2"
android:orientation="horizontal"
android:gravity="center"
android:layout_gravity="center">
<TimeTableKeeper.Tile
android:layout_width="75dp"
android:layout_height="75dp"
android:background="#00FF00"
custom:color="green"
custom:layout_row="0"
custom:layout_column="0" />
<TimeTableKeeper.Tile
android:layout_width="75dp"
android:layout_height="75dp"
android:background="#00FF00"
custom:color="blue"
custom:layout_row="0"
custom:layout_column="1" />
</GridLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud) 我有一个GridLayout由6个孩子组成的.它具有2的列数左栏的孩子有一个layout_gravity的start|end|fill_horizontal和layout_width0dp的,这会导致他们填写所有可用空间.
这很棒,并给出了如下所示的输出.

视图4,5和6只有它们需要的大小.很酷.我现在想将视图4,5和6对准容器的右侧,但事实证明这很有挑战性.由于的方式GridLayout决定了列灵活性,让右列的意见layout_gravity的right废墟布局.第一列中的视图不再填充布局中的剩余宽度.(根据我的理解,这是因为现在两列都有子项定义水平重力,所以GridLayout不能再将一列标记为灵活).
有没有办法实现这个目标?我真的不喜欢筑巢LinearLayout.以下是我想要的结果.

作为参考,当前的视图层次结构XML看起来像......
<GridLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="2">
<View
android:id="@+id/view1"
android:layout_gravity="start|end|fill_horizontal" />
<View
android:id="@+id/view4" />
<View
android:id="@+id/view2"
android:layout_gravity="start|end|fill_horizontal" />
<View
android:id="@+id/view5" />
<View
android:id="@+id/view3"
android:layout_gravity="start|end|fill_horizontal" />
<View
android:id="@+id/view6" />
</GridLayout>
Run Code Online (Sandbox Code Playgroud) 我想在使用RecyclerView时在GridLayoutManager的行中显示可变数量的列.要显示的列数取决于列的TextView的大小.
我不知道列宽是否正在动态放入文本中.
有人可以帮忙吗?StaggeredGridLayoutManager没有解决我的目的,因为它自定义高度但是需要固定数量的列.
android android-gridlayout staggeredgridlayout android-recyclerview
我实际上在我的应用程序中使用了一个带有两列的GridLayoutManager,我希望根据所使用的视图类型有一列.
这是我片段的方法"onCreateView()"中的代码:
// Recycler view for users
usersList = (RecyclerView) mView.findViewById(R.id.usersList);
// Layout manager (grid of users
layoutManager = new GridLayoutManager(mContext, 2);
usersList.setLayoutManager(layoutManager);
// ArrayList of users
users = new ArrayList<User>();
// Set the adapter for the list of users
mHomeListAdapter = new HomeListAdapter(getActivity(), users);
usersList.setAdapter(mHomeListAdapter);
Run Code Online (Sandbox Code Playgroud)
然后,在我的适配器中,我有这样的代码:
@Override
public int getItemViewType(int position) {
if (position == 5) {
return TYPE_PREMIUM;
} else {
return TYPE_ITEM;
}
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == TYPE_PREMIUM) { …Run Code Online (Sandbox Code Playgroud)