最近很多小伙伴都在问pyside-如何从gridLayout删除小部件和gridview删除这两个问题,那么本篇文章就来给大家详细解答一下,同时本文还将给你拓展androidGridLayout、An
最近很多小伙伴都在问pyside - 如何从gridLayout删除小部件和gridview删除这两个问题,那么本篇文章就来给大家详细解答一下,同时本文还将给你拓展android GridLayout、Android GridLayout API 21、android gridlayout实现、Android – Gridlayout android.support.v7.widget.GridLayout无法强制转换为android.widget.GridLayout等相关知识,下面开始了哦!
本文目录一览:- pyside - 如何从gridLayout删除小部件(gridview删除)
- android GridLayout
- Android GridLayout API 21
- android gridlayout实现
- Android – Gridlayout android.support.v7.widget.GridLayout无法强制转换为android.widget.GridLayout
pyside - 如何从gridLayout删除小部件(gridview删除)
我在QT Designer中构建了一个ui,然后使用pyside-uic将其转换为python
文件,然后编写了一些代码以编程方式对其进行编辑。在
otherwords我有一个按钮Add Row
,单击时将自身重新命名
,以Remove1
创造另一个pusbutton
命名Add Row
,并把它添加到
布局。
单击时的代码Add Row,更改名称和信号/插槽:
self.pb_Row[-1].setText('Remove'+str(self.nRows))
self.pb_Row[-1].clicked.disconnect( self.addRow )
self.pb_Row[-1].clicked.connect( self.removeRow )
Code when clicking Remove
,removes selected button:
iRow = int(self.sender().objectName().split('_')[-1])-1
ind = self.PropertyLayout.indexOf(self.pb_Row[iRow])
t = self.PropertyLayout.takeAt(ind)
t.widget().deleteLater()
# self.pb_Row[iRow].hide()
# self.pb_Row[iRow].deleteLater()
self.pb_Row.pop(iRow)
直到您添加至少一个然后将其删除,这才可以正常工作,下次再将
其弄乱。基本上,当我有两个按钮并
删除一个按钮然后尝试添加一个按钮时,它的行为不正常。行为不端是指新按钮
结束于旧按钮的顶部,有时出现在下方而不是上方。
另外,按照当前的行
数,如果我使用.hide()它的话,它实际上并不会真正重组gridlayout中的内容。我不太确定
应该使用哪个。
谢谢!
这是一个产生不良结果的序列:
新的开始 图片1
单击添加后 单击添加后
单击“删除”(到目前为止一切正常),然后单击“添加”(无明显
差异)删除1后
单击第二次添加后 在此处输入图片说明
单击Remove2后,Remove1从其下方显示 在此处输入图片说明
代码的“工作”示例
import numpy as np
import sys
from PySide import QtCore,QtGui
import matplotlib.pyplot as plt
from ModesInFiber import Ui_fiberModesMainWindow
class Ui_fiberModesMainWindow(object):
def setupUi(self,fiberModesMainWindow):
fiberModesMainWindow.setObjectName("fiberModesMainWindow")
fiberModesMainWindow.resize(653,597)
self.centralwidget = QtGui.QWidget(fiberModesMainWindow)
self.centralwidget.setObjectName("centralwidget")
self.horizontalLayout_2 = QtGui.QHBoxLayout(self.centralwidget)
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.MainLayout = QtGui.QGridLayout()
self.MainLayout.setObjectName("MainLayout")
self.PropertyLayout = QtGui.QGridLayout()
self.PropertyLayout.setObjectName("PropertyLayout")
self.lbl_Name = QtGui.QLabel(self.centralwidget)
self.lbl_Name.setObjectName("lbl_Name")
self.PropertyLayout.addWidget(self.lbl_Name,1,1)
self.pb_addRow_1 = QtGui.QPushButton(self.centralwidget)
self.pb_addRow_1.setObjectName("pb_addRow_1")
self.PropertyLayout.addWidget(self.pb_addRow_1,5,1)
self.ledit_Name_1 = QtGui.QLineEdit(self.centralwidget)
self.ledit_Name_1.setObjectName("ledit_Name_1")
self.PropertyLayout.addWidget(self.ledit_Name_1,1)
self.MainLayout.addLayout(self.PropertyLayout,1)
spacerItem2 = QtGui.QSpacerItem(20,40,QtGui.QSizePolicy.Minimum,QtGui.QSizePolicy.Expanding)
self.MainLayout.addItem(spacerItem2,1)
self.horizontalLayout_2.addLayout(self.MainLayout)
fiberModesMainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(fiberModesMainWindow)
QtCore.QMetaObject.connectSlotsByName(fiberModesMainWindow)
# fiberModesMainWindow.setTabOrder(self.ledit_Name_1,self.ledit_Width_1)
# fiberModesMainWindow.setTabOrder(self.ledit_Width_1,self.cmb_RIType_1)
# fiberModesMainWindow.setTabOrder(self.cmb_RIType_1,self.ledit_RIParam_1)
# fiberModesMainWindow.setTabOrder(self.ledit_RIParam_1,self.pb_addRow_1)
def retranslateUi(self,fiberModesMainWindow):
fiberModesMainWindow.setWindowTitle(QtGui.QApplication.translate("fiberModesMainWindow","MainWindow",None,QtGui.QApplication.UnicodeUTF8))
self.lbl_Name.setText(QtGui.QApplication.translate("fiberModesMainWindow","Name",QtGui.QApplication.UnicodeUTF8))
self.pb_addRow_1.setText(QtGui.QApplication.translate("fiberModesMainWindow","Add Row",QtGui.QApplication.UnicodeUTF8))
class DesignerMainWindow(QtGui.QMainWindow,Ui_fiberModesMainWindow):
def __init__(self,parent = None):
super(DesignerMainWindow,self).__init__(parent)
self.setupUi(self)
self.pb_addRow_1.clicked.connect( self.addRow )
self.ledit_Name = [ self.ledit_Name_1 ]
self.pb_Row = [ self.pb_addRow_1 ]
# number of rows
self.nRows = 1
def addRow( self ):
self.ledit_Name[-1].setEnabled(False)
self.pb_Row[-1].setText('Remove'+str(self.nRows))
self.pb_Row[-1].clicked.disconnect( self.addRow )
self.pb_Row[-1].clicked.connect( self.removeRow )
self.nRows += 1
self.ledit_Name.append( QtGui.QLineEdit(self.centralwidget) )
self.ledit_Name[-1].setObjectName('ledit_Name_'+str(self.nRows))
self.PropertyLayout.addWidget( self.ledit_Name[-1],self.nRows,1)
self.pb_Row.append( QtGui.QPushButton(self.centralwidget) )
self.pb_Row[-1].setObjectName( 'pb_addRow_'+str(self.nRows) )
self.pb_Row[-1].setText('Add Row')
self.pb_Row[-1].clicked.connect( self.addRow )
self.PropertyLayout.addWidget( self.pb_Row[-1],1)
def removeRow( self ):
iRow = int(self.sender().objectName().split('_')[-1])-1
self.nRows -= 1
ind = self.PropertyLayout.indexOf(self.ledit_Name[iRow])
t = self.PropertyLayout.takeAt(ind)
t.widget().setParent(None)
# t.widget().deleteLater()
# self.ledit_Name[iRow].hide()
# self.ledit_Name[iRow].deleteLater()
# self.ledit_Name[iRow].setParent(None)
self.ledit_Name.pop(iRow)
ind = self.PropertyLayout.indexOf(self.pb_Row[iRow])
t = self.PropertyLayout.takeAt(ind)
t.widget().setParent(None)
# t.widget().deleteLater()
# self.pb_Row[iRow].hide()
# self.pb_Row[iRow].deleteLater()
# self.pb_Row[iRow].setParent(None)
self.pb_Row.pop(iRow)
for iAfterRow in range(iRow,self.nRows):
self.ledit_Name[iAfterRow].setObjectName( 'ledit_Name_' + str(iAfterRow+1) )
self.pb_Row[iAfterRow].setObjectName( 'ledit_Name_' + str(iAfterRow+1) )
print 'Remove row',iRow
if __name__ == '__main__':
app = QtGui.QApplication( sys.argv )
dmw = DesignerMainWindow()
dmw.show()
sys.exit( app.exec_() )
android GridLayout
结构
继承关系
public class GridLayout extends ViewGroup
java.lang.Object
android.view.View
android.view.ViewGroup
android.widget.GridLayout
该布局把子视图存放在一个矩形网格中。
网格是由被无数虚细线分割成多个单元格的可视区域组成。贯穿整个 API 的网格线通过网格索引数来指定。一个 N 列的网格在运行中包含 0 到 N 的 N+1 个索引,不管怎么配置 GridLayout 网格索引 0 是固定网格容器的前边距,索引 N 是固定容器的后边距(考虑后填充)。
行和列的规格
在 rowSpec 和 columnSpec 布局参数的定义后,子视图占用一个或者多个连续单元格,每个规范是定义被占用的行或列的设置和子视图在单元格是如何对齐。尽管各个单元格在一个 GridLayout 中不重叠,GridLayout 并没有阻止子视图被定义为占据相同的单元格或者单元格组。然而在这种情况下,也不能保证子视图在布局操作完成后自己不会重叠。
默认单元格分配
如果一个子视图没有指定占据的行和列索引,GridLayout 会自动指定单元格位置,包括:方向,行数和列数的属性。
空间 (Place)
子视图之间的空间可能会通过使用专用的空间视图的实例,或通过设置 leftMargin,topMargin,rightMargin 和 bottomMargin 布局参数后指定。当设置为 useDefaultMargins 属性,根据当前平台的用户界面风格,子视图周围的默认边距将自动分配。每个被定义的边距可通过分配到相应的布局参数来独立覆盖。默认值通常在不同组成部分会产生一个合理的间距,但在不同平台版本之间可能会改变。
多余的空间分布
GridLayout 的多余的空间分布是基于优先级,而不是根据比例。
一个子视图的的伸展程度通过其行和列的组的对其属性推算(这是典型的设置子视图的布局参数 gravity 属性)。如果对齐是沿着给定的轴定义,那么该组件在这个方向具有灵活性。如果没有对齐,相反组件会缺乏灵活性。
多个组件在同一行或列组被认为平行的。如果组中所有在内的组件是灵活的,那么这个小组是灵活的。位置在一个共同的边界两侧的行和列组,反而认为采取同一系列。如果复合组的一个元素是灵活的,则这个复合组是灵活的。
为了使一列伸展,确保所有的组件,它里面定义一个的 gravity 属性。为了防止从列伸展,确保列中的组成部分之一没有定义的 gravity 属性。
灵活性的原则并不能完全的消除歧义,推荐 GridLayout 中的算法更接近其右侧和底边的行和列。
局限性
GridLayout 的不提供支持空间 (weight) 分配的原则,空间分配界定。在一般情况下,可以配置一个 GridLayout 多余的空间分布在多个行或列之间的不相同的比例。
一些常见的适用情况,如下:将等量的周围空间中的一个组成部分单元格组 ; 使用居中对齐(或 gravity 属性)。对于完全控制对或列中多余的空间分布 ; 使用的 LinearLayout 子视图约束相关的单元格组的组成部分。当使用这些技术,记住,单元格组可能定义重叠。
为 GridLayout 的使用布局参数的完整描述 GridLayout.LayoutParams。
XML 属性
属性名称 |
描述 |
|||||||||
android:alignmentMode |
当设置 alignMargins, 使视图的外边界之间进行校准,定义其边距,当设置 alignBounds,使视图的边界之间进行校准,默认设置 alignMargins。
|
|||||||||
android:columnCount |
自动定位子视图时创建的最大列数 |
|||||||||
android:columnOrderPreserved |
当设置为 true,使列边界显示的顺序和列索引的顺序相同。默认是 true。 |
|||||||||
android:orientation |
Orientation 属性在布局时候不被使用,它仅当子视图布局参数没有指定的时候分配行和列,GridLayout 在这种情况下和 LinearLayout 使用方法一样,根据标志的值将所有组件放在单个行或者放在当个列中。在水平情况下,当一行的所有列都填充满时,columnCount 属性额外提供创建新行。同样在垂直情况下,rowCount 属性有相同的作用,默认是水平的。
|
|||||||||
android:rowCount |
自动定位子视图时创建的最大行数 |
|||||||||
android:rowOrderPreserved |
当设置为 true,使行边界显示的顺序和行索引的顺序相同。默认是 true。 |
|||||||||
android:useDefaultMargins |
当设置 ture,当没有指定视图的布局参数时,告诉 GridLayout 使用默认的边距。默认值是 false。 |
常量
类型 |
属性名称 |
描述 |
int |
ALIGN_BOUNDS |
这是 alignmentMode 的常量,当 alignmentMode 设置成 ALIGN_BOUNDS,每个组件之间的原始视图边界边距对齐:即组件的区域通过下面分隔:顶部,左侧,底部和右侧的属性。默认值:0 |
int |
ALIGN_MARGINS |
这是 alignmentMode 的常量。当 alignmentMode 设置 ALIGN_MARGINS,每个视图的边界向外延伸,根据他们的边距,产生的矩形边距对齐。默认值:1 |
int |
HORIZONTAL |
水平方向。默认值:0 |
int |
UNDEFINED |
常数用于表示一个不确定的值。该字段可以使用这个值来表示他们的值观尚未确定。同样的方法可以返回这个值,表明没有能实现返回的合适值,该常数的值(目前 MIN_VALUE)是为了避免可能不知道其标志的有效值之间的混淆。 |
int |
VERTICAL |
垂直方向。默认值:1 |
字段
public static final GridLayout.Alignment |
|
属性名称 |
描述 |
BASELINE |
表示一个视图应该和在同一单元格组的其他视图基线对齐,这个常量仅用做 rowSpecs 对齐 |
BOTTOM |
表示一个视图应该和在同一单元格组的其他视图底部边距对齐。 |
CENTER |
表示一个视图应该和在同一单元格组的其他视图居中对齐,此常数可用于 rowSpecs 和 columnSpecs。 |
FILL |
表示视图应该扩大以填充其单元格组。此常数可用于 rowSpecs 和 columnSpecs。 |
LEFT |
表示一个视图应该和在同一单元格组的其他视图左边距对齐。 |
RIGHTT |
表示一个视图应该和在同一单元格组的其他视图右边距对齐。 |
TOP |
表示一个视图应该和在同一单元格组的其他视图上边距对齐。 |
公共方法
public GridLayout.LayoutParams generateLayoutParams (AttributeSet attrs)
在提供的属性集基础上返回一个新的布局参数设置。
参数
attrs 创建布局参数的属性
返回值
ViewGroup.LayoutParams 实例或者他的派生实例
public int getAlignmentMode ()
返回对齐方式.
相关 XML 属性
android:alignmentMode
返回值
对齐方式 ; ALIGN_BOUNDS
或者 ALIGN_MARGINS
参见
ALIGN_BOUNDS
ALIGN_MARGINS
setAlignmentMode(int)
public int getColumnCount ()
返回当前的列数。通过 setColumnCount(int)方法最后一次设置的值,如果没有这样的值被设置,返回在 columnSpec 定义中的每一个上限的最大值。
相关 XML 属性
android:columnCount
返回值
当前的列数
参见
setColumnCount(int)
columnSpec
public int getOrientation ()
返回当前方向。
相关 XML 属性
android:orientation
返回值
HORIZONTAL
或者 VERTICAL
参见
setOrientation(int)
public int getRowCount ()
返回当前的行数。通过 setRowCount(int)方法最后一次设置的值,如果没有这样的值被设置,返回在 rowSpec 定义中的每一个上限的最大值。
相关 XML
android:rowCount
返回值
返回当前的行数。
参见
setRowCount(int)
rowSpec
public boolean getUseDefaultMargins ()
返回是否 GridLayout 分配时候,没有相应布局参数定义的默认边距。
相关 XML 属性
android:useDefaultMargins
返回值
如果分配默认的边距返回 ture
参见
setUseDefaultMargins(boolean)
public boolean isColumnOrderPreserved ()
返回是否通过表格索引顺序定制列边界。
相关 XML 属性
android:columnOrderPreserved
返回值
如果列边界按照索引的顺序显示返回 ture,否则返回 false
参见
setColumnOrderPreserved(boolean)
public boolean isRowOrderPreserved ()
返回是否通过表格索引顺序定制行边界
相关 XML 属性
android:rowOrderPreserved
返回值
如果行边界按照索引顺序显示返回 ture,否则返回 false
参见
setRowOrderPreserved(boolean)
public void requestLayout ()
当无效的视图布局发生变化时候调用它,将通过视图树进行布局传递。
public void setAlignmentMode (int alignmentMode)
设置该容器中所有子视图之间的对齐方式
默认的值是 ALIGN_MARGINS.
相关 XML 属性
android:alignmentMode
参数
alignmentMode ALIGN_BOUNDS 或者 ALIGN_MARGINS
参见
ALIGN_BOUNDS
ALIGN_MARGINS
getAlignmentMode()
public void setColumnCount (int columnCount)
列数是仅用于当没有一个布局参数指定时候,生成默认的列 / 行索引。
相关 XML 属性
android:columnCount
参数
columnCount 列的数量.
参见
getColumnCount()
columnSpec
public void setColumnOrderPreserved (boolean columnOrderPreserved)
当此属性为 true,GridLayout 的是被迫使他们相关的网格指数在视图中以升序顺序放置列的边界。
当此属性是 false,GridLayout 是放置在任何最适合给定的约束水平列边界的顺序。
此属性的默认值是 true。
相关 XML 属性
android:columnOrderPreserved
参数
columnOrderPreserved 用 ture 是被迫使他们相关的网格指数在视图中以升序顺序放置列的边界。
参见
isColumnOrderPreserved()
public void setOrientation (int orientation)
Orientation 是仅用于当没有一个布局参数指定时候,生成默认的列 / 行索引
默认的属性值是 HORIZONTAL
相关 XML 属性
android:orientation
参数
orientation HORIZONTAL 或者 VERTICAL
参见
getOrientation()
public void setRowCount (int rowCount)
RowCount 是仅用于当没有一个布局参数指定时候,生成默认的列 / 行索引
相关 XML 属性
android:rowCount
参数
rowCount 行数
参见
getRowCount()
rowSpec
public void setRowOrderPreserved (boolean rowOrderPreserved)
当此属性为 true,GridLayout 是强制他们相关的网格指数在视图中以升序顺序放置行的边界。
当此属性是 false,GridLayout 是放置在任何最适合给定的约束水平行边界的顺序。
此属性的默认值是 true。
相关 XML 属性
android:rowOrderPreserved
参数
rowOrderPreserved 用 ture 是强制他们相关的网格指数在视图中以升序顺序放置列的边界。
参见
isRowOrderPreserved ()
public void setUseDefaultMargins (boolean useDefaultMargins)
当设置为 true, GridLayout 根据子视图的的视觉特征分配在子视图周围的默认边距,每个定义的边距,可独立分配到相应的布局参数覆盖。
如果为 false,所有边距的默认值是零。
当设置为 true 时,考虑设置的 alignmentMode 属性值 ALIGN_BOUNDS。
此属性的默认值是 false。
相关 XML 属性
android:useDefaultMargins
参数
useDefaultMargins 使用 true 分配默认的边距
参见
getUseDefaultMargins()
setAlignmentMode(int)
public static GridLayout.Spec spec (int start, GridLayout.Alignment alignment)
返回一个 Spec,其中:
l spec.span = [start, start + 1]
l spec.alignment = alignment
参数
start the 开始索引
alignment 对齐方式
public static GridLayout.Spec spec (int start, int size)
返回一个 Spec,其中:
l spec.span = [start, start + size]
参数
start 开始位置
size 大小
public static GridLayout.Spec spec (int start, int size, GridLayout.Alignment alignment)
返回一个 Spec,其中:
l spec.span = [start, start + size]
l spec.alignment = alignment
参数
start 开始位置
size 大小
alignment 对齐方式
public static GridLayout.Spec spec (int start)
返回一个 Spec,其中:
l spec.span = [start, start + 1]
参数
start 开始索引
保护方法
protected GridLayout.LayoutParams generateDefaultLayoutParams ()
返回一组默认布局参数。没有设置布局参数执行 addview(View)时,这些参数被请求时返回 null,并抛出一个异常。
返回值
一组默认布局参数或者 null
protected GridLayout.LayoutParams generateLayoutParams (ViewGroup.LayoutParams p)
返回一组合法的受支持的布局参数。当一个 ViewGroup 传递一个布局参数没有通过 checkLayoutParams(android.view.ViewGroup.LayoutParams) 检测的视图时,此方法被调用。此方法会返回一组新的适合当前 ViewGroup 的布局参数,可能从指定的一组布局参数中复制适当的属性。
参数
p 被转换成一组适合当前 ViewGroup 的布局参数.
返回值
一个 ViewGroup.LayoutParams 的实例或者其中的一个子节点
补充
文章精选
Android 4.0 新增 Space 及GridLayout 初谈
☆ ☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆
Android GridLayout API 21
目前,我手动设置瓷砖的宽度等于屏幕宽度的一半.这样做效果很好,但是它会在拼贴之间添加分隔线(如屏幕截图所示).幸运的是,似乎在API 21,there is now support for weight in GridLayout中引用了这里以方便您:
As of API 21,GridLayout’s distribution of excess space accomodates
the principle of weight. In the event that no weights are specified,
the prevIoUs conventions are respected and columns and rows are taken
as flexible if their views specify some form of alignment within their
groups. The flexibility of a view is therefore influenced by its
alignment which is,in turn,typically defined by setting the gravity
property of the child’s layout parameters. If either a weight or
alignment were defined along a given axis then the component is taken
as flexible in that direction. If no weight or alignment was set,the
component is instead assumed to be inflexible.Multiple components in the same row or column group are considered to
act in parallel. Such a group is flexible only if all of the
components within it are flexible. Row and column groups that sit
either side of a common boundary are instead considered to act in
series. The composite group made of these two elements is flexible if
one of its elements is flexible.To make a column stretch,make sure all of the components inside it
define a weight or a gravity. To prevent a column from stretching,
ensure that one of the components in the column does not define a
weight or a gravity.When the principle of flexibility does not provide complete
disambiguation,GridLayout’s algorithms favour rows and columns that
are closer to its right and bottom edges. To be more precise,
GridLayout treats each of its layout parameters as a constraint in the
a set of variables that define the grid-lines along a given axis.
During layout,GridLayout solves the constraints so as to return the
unique solution to those constraints for which all variables are
less-than-or-equal-to the corresponding value in any other valid
solution.
我将这个由GridLayout和2列组成的简单布局放在一起,尝试实现两个同等宽度的列:
<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <GridLayout android:layout_width="match_parent" android:layout_height="fill_parent" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:columnCount="2"> <TextView android:layout_width="wrap_content" android:layout_gravity="fill_horizontal" android:text="Hello" android:layout_columnWeight="1" /> <TextView android:layout_width="wrap_content" android:layout_gravity="fill_horizontal" android:text="Hello" android:layout_columnWeight="1" /> <TextView android:layout_width="wrap_content" android:layout_gravity="fill_horizontal" android:text="Hello" android:layout_columnWeight="1" /> <TextView android:layout_width="wrap_content" android:layout_gravity="fill_horizontal" android:text="Hello" android:layout_columnWeight="1" /> <TextView android:layout_width="wrap_content" android:layout_gravity="fill_horizontal" android:text="Hello" android:layout_columnWeight="1" /> </GridLayout> </RelativeLayout>
我认为,因为所有的孩子都有相同的柱重,所以会产生同样宽的柱子.然而,情况似乎并非如此,因为这是结果:
第一列比第二列窄.这里有什么我想念的吗?我是否误解了GridLayout的重量如何?
作为另一个问题,我似乎不能用重量技巧做0宽度以确保相同的宽度而不管内容(TextView只是消失).如何使用GridLayout完成此操作?
解决方法
如果您具有指定宽度大小的子视图,请确保没有子视图的宽度大于平均宽度大小.
希望它会对你有所帮助.
android gridlayout实现
我正在尝试开发一个新项目.我的客户希望我设计当前的GooglePlay设计(每个附件的块设计).这就是为什么我很困惑< android.support.v7.widget.GridLayout>对于实现这样的块设计特征是否有用.如果没有,请告诉我怎么做?
解决方法:
你可能想看看http://developer.android.com/design/building-blocks/grid-lists.html.不完全确定你想要做什么,但我希望这会有所帮助.它使用GridView而不是GridLayout.关于这一点的好帖子可以在GridView VS GridLayout in Android Apps找到
编辑:这是我的例子
注意:大部分内容取自http://developer.android.com/guide/topics/ui/layout/gridview.html. GridViewExample中使用的ImageAdapter直接从那里获取,就像使用的图片一样.
这是我的布局
<?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:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:textColor="@android:color/black"
android:textandroid:textSize="50sp"
android:gravity="center_horizontal"
android:text="Page Title" />
<!-- Insert some sort of scrolling if desired -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/black"
android:textColor="@android:color/white"
android:textSize="25sp"
android:gravity="center_horizontal"
android:text="Grid 1 Title" />
<GridView
android:id="@+id/gridView1"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:columnWidth="50dp"
android:numColumns="auto_fit"
android:verticalSpacing="5dp"
android:horizontalSpacing="5dp"
android:stretchMode="columnWidth"
android:gravity="center" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/black"
android:textColor="@android:color/white"
android:textSize="25sp"
android:gravity="center_horizontal"
android:text="Grid 2 Title" />
<GridView
android:id="@+id/gridView2"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:columnWidth="50dp"
android:numColumns="auto_fit"
android:verticalSpacing="5dp"
android:horizontalSpacing="5dp"
android:stretchMode="columnWidth"
android:gravity="center" />
</LinearLayout>
</LinearLayout>
这是我的主要活动:
package com.example.gridviewdemo;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.Toast;
public class GridViewExample extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gridview_example);
GridView gridView1 = (GridView) findViewById(R.id.gridView1);
gridView1.setAdapter(new ImageAdapter(this));
gridView1.setonItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Toast.makeText(GridViewExample.this, "1: " + position, Toast.LENGTH_SHORT).show();
}
});
GridView gridView2 = (GridView)findViewById(R.id.gridView2);
gridView2.setAdapter(new ImageAdapter(this));
gridView2.setonItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> parent, View v, int position, long id){
Toast.makeText(GridViewExample.this, "2: " + position, Toast.LENGTH_SHORT).show();
}
});
}
}
生产:
看起来平板电脑的布局有点偏,但有些工作我肯定可以修复
Android – Gridlayout android.support.v7.widget.GridLayout无法强制转换为android.widget.GridLayout
我想创建一个能够在所有API中运行的GridLayout.
问题是,当我使用GridLayout而不是android.support.v7.widget.GridLayout时,该应用程序在Android 7.1.1中正常运行,但在旧版本中它崩溃了.
但是,如果我使用android.support.v7.widget.GridLayout而不是GridLayout(如下面的代码),它总是崩溃.
activity_main
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#FFF"
tools:context="devector.dom.gridtest.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textGrid"
android:text="Grid test"
android:textColor="#000"
android:layout_centerInParent="true"
android:textSize="34sp"
/>
</RelativeLayout>
<android.support.v7.widget.GridLayout
android:textAlignment="center"
android:id="@+id/mainGrid"
android:padding="25dp"
android:layout_marginLeft="8dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:columnCount="2"
app:rowCount="3"
app:alignmentMode="alignMargins"
app:columnorderPreserved="false">
<!-- Row 1 -->
<!-- Column 1-->
<android.support.v7.widget.CardView
android:layout_width="150dp"
android:layout_height="125dp"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
app:cardElevation="8dp"
app:cardCornerRadius="8dp"
>
<LinearLayout
android:layout_gravity="center_horizontal|center_vertical"
android:layout_margin="16dp"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<ImageView
android:src="@drawable/zerotwo"
android:layout_gravity="center_horizontal"
android:layout_width="70dp"
android:layout_height="70dp"
/>
<TextView
android:text="Zero"
android:textAlignment="center"
android:textColor="#000"
android:textSize="18sp"
android:textandroid:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</android.support.v7.widget.CardView>
<!-- Row 2 -->
<!-- Column 2-->
<android.support.v7.widget.CardView
android:layout_width="150dp"
android:layout_height="125dp"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
app:cardElevation="8dp"
app:cardCornerRadius="8dp"
>
<LinearLayout
android:layout_gravity="center_horizontal|center_vertical"
android:layout_margin="16dp"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<ImageView
android:src="@drawable/zerotwo"
android:layout_gravity="center_horizontal"
android:layout_width="70dp"
android:layout_height="70dp"
/>
<TextView
android:text="Two"
android:textAlignment="center"
android:textColor="#000"
android:textSize="18sp"
android:textandroid:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</android.support.v7.widget.CardView>
<!-- Row 3-->
<!-- Column 3-->
<android.support.v7.widget.CardView
android:layout_width="150dp"
android:layout_height="125dp"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
app:cardElevation="8dp"
app:cardCornerRadius="8dp"
>
<LinearLayout
android:layout_gravity="center_horizontal|center_vertical"
android:layout_margin="16dp"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<ImageView
android:src="@drawable/zerotwo"
android:layout_gravity="center_horizontal"
android:layout_width="70dp"
android:layout_height="70dp"
/>
<TextView
android:text="Is"
android:textAlignment="center"
android:textColor="#000"
android:textSize="18sp"
android:textandroid:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</android.support.v7.widget.CardView>
<!-- Row 4 -->
<!-- Column 4-->
<android.support.v7.widget.CardView
android:layout_width="150dp"
android:layout_height="125dp"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
app:cardElevation="8dp"
app:cardCornerRadius="8dp"
>
<LinearLayout
android:layout_gravity="center_horizontal|center_vertical"
android:layout_margin="16dp"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<ImageView
android:src="@drawable/zerotwo"
android:layout_gravity="center_horizontal"
android:layout_width="70dp"
android:layout_height="70dp"
/>
<TextView
android:text="The"
android:textAlignment="center"
android:textColor="#000"
android:textSize="18sp"
android:textandroid:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</android.support.v7.widget.CardView>
<!-- Row 5 -->
<!-- Column 5-->
<android.support.v7.widget.CardView
android:layout_width="150dp"
android:layout_height="125dp"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
app:cardElevation="8dp"
app:cardCornerRadius="8dp"
>
<LinearLayout
android:layout_gravity="center_horizontal|center_vertical"
android:layout_margin="16dp"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<ImageView
android:src="@drawable/zerotwo"
android:layout_gravity="center_horizontal"
android:layout_width="70dp"
android:layout_height="70dp"
/>
<TextView
android:text="Besto"
android:textAlignment="center"
android:textColor="#000"
android:textSize="18sp"
android:textandroid:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</android.support.v7.widget.CardView>
<!-- Row 6 -->
<!-- Column 6-->
<android.support.v7.widget.CardView
android:layout_width="150dp"
android:layout_height="125dp"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
app:cardElevation="8dp"
app:cardCornerRadius="8dp"
>
<LinearLayout
android:layout_gravity="center_horizontal|center_vertical"
android:layout_margin="16dp"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<ImageView
android:src="@drawable/zerotwo"
android:layout_gravity="center_horizontal"
android:layout_width="70dp"
android:layout_height="70dp"
/>
<TextView
android:text="Waifu"
android:textAlignment="center"
android:textColor="#000"
android:textSize="18sp"
android:textandroid:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</android.support.v7.widget.CardView>
<!-- Row 7 -->
<!-- Column 7-->
<android.support.v7.widget.CardView
android:layout_width="150dp"
android:layout_height="125dp"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
app:cardElevation="8dp"
app:cardCornerRadius="8dp"
>
<LinearLayout
android:layout_gravity="center_horizontal|center_vertical"
android:layout_margin="16dp"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<ImageView
android:src="@drawable/zerotwo"
android:layout_gravity="center_horizontal"
android:layout_width="70dp"
android:layout_height="70dp"
/>
<TextView
android:text="Never"
android:textAlignment="center"
android:textColor="#000"
android:textSize="18sp"
android:textandroid:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</android.support.v7.widget.CardView>
<!-- Row 8 -->
<!-- Column 8-->
<android.support.v7.widget.CardView
android:layout_width="150dp"
android:layout_height="125dp"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
app:cardElevation="8dp"
app:cardCornerRadius="8dp"
>
<LinearLayout
android:layout_gravity="center_horizontal|center_vertical"
android:layout_margin="16dp"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<ImageView
android:src="@drawable/zerotwo"
android:layout_gravity="center_horizontal"
android:layout_width="70dp"
android:layout_height="70dp"
/>
<TextView
android:text="Forget"
android:textAlignment="center"
android:textColor="#000"
android:textSize="18sp"
android:textandroid:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</android.support.v7.widget.CardView>
<!-- Row 9 -->
<!-- Column 9-->
<android.support.v7.widget.CardView
android:layout_width="150dp"
android:layout_height="125dp"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
app:cardElevation="8dp"
app:cardCornerRadius="8dp"
>
<LinearLayout
android:layout_gravity="center_horizontal|center_vertical"
android:layout_margin="16dp"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<ImageView
android:src="@drawable/zerotwo"
android:layout_gravity="center_horizontal"
android:layout_width="70dp"
android:layout_height="70dp"
/>
<TextView
android:text="About"
android:textAlignment="center"
android:textColor="#000"
android:textSize="18sp"
android:textandroid:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</android.support.v7.widget.CardView>
<!-- Row 10 -->
<!-- Column 10-->
<android.support.v7.widget.CardView
android:layout_width="150dp"
android:layout_height="125dp"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
app:cardElevation="8dp"
app:cardCornerRadius="8dp"
>
<LinearLayout
android:layout_gravity="center_horizontal|center_vertical"
android:layout_margin="16dp"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<ImageView
android:src="@drawable/zerotwo"
android:layout_gravity="center_horizontal"
android:layout_width="70dp"
android:layout_height="70dp"
/>
<TextView
android:text="It"
android:textAlignment="center"
android:textColor="#000"
android:textSize="18sp"
android:textandroid:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</android.support.v7.widget.CardView>
</android.support.v7.widget.GridLayout>
</LinearLayout>
</ScrollView>
Logs
FATAL EXCEPTION: main
Process: devector.dom.gridtest, PID: 15773
java.lang.RuntimeException: Unable to start activity ComponentInfo{devector.dom.gridtest/devector.dom.gridtest.MainActivity}: java.lang.classCastException: android.support.v7.widget.GridLayout cannot be cast to android.widget.GridLayout
at android.app.ActivityThread.performlaunchActivity(ActivityThread.java:2678)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2743)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1490)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6165)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:778)
Caused by: java.lang.classCastException: android.support.v7.widget.GridLayout cannot be cast to android.widget.GridLayout
at devector.dom.gridtest.MainActivity.onCreate(MainActivity.java:21)
at android.app.Activity.performCreate(Activity.java:6687)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1140)
at android.app.ActivityThread.performlaunchActivity(ActivityThread.java:2631)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2743)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1490)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6165)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:778)
Gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
defaultConfig {
applicationId "devector.dom.gridtest"
minSdkVersion 15
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation filetree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support:cardview-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
implementation 'com.android.support:gridlayout-v7:26.1.0'
implementation 'com.android.support:design:26.1.0'
}
MainActivity.java
package devector.dom.gridtest;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.CardView;
import android.view.View;
import android.widget.GridLayout;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
GridLayout mainGrid;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainGrid = (GridLayout) findViewById(R.id.mainGrid);
setSingleEvent(mainGrid);
}
private void setSingleEvent(GridLayout mainGrid) {
for (int i = 0; i < mainGrid.getChildCount(); i++) {
CardView cardView = (CardView) mainGrid.getChildAt(i);
final int finalI = i;
cardView.setonClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(finalI == 0)
{
Intent intent = new Intent(MainActivity.this,Activity_one.class );
startActivity(intent);
}
}
});
}
}
}
解决方法:
从您的错误日志中是,
android.support.v7.widget.GridLayout cannot be cast to android.widget.GridLayout
at devector.dom.gridtest.MainActivity.onCreate(MainActivity.java:21)
你可以像这样改变你的演员来解决你的问题
android.support.v7.widget.GridLayout grid = (android.support.v7.widget.GridLayout)findViewById(R.id.grid);
今天关于pyside - 如何从gridLayout删除小部件和gridview删除的分享就到这里,希望大家有所收获,若想了解更多关于android GridLayout、Android GridLayout API 21、android gridlayout实现、Android – Gridlayout android.support.v7.widget.GridLayout无法强制转换为android.widget.GridLayout等相关知识,可以在本站进行查询。
本文标签: