如何在GridLayout中垂直滚动?我已经尝试使用ScrollView将GridLayout作为子项,在GridLayout中使用RelativeLayout子项,但这会导致所有子项堆叠在一起并且或者超出布局.
在ScrollView中使用GridLayout之前:

在ScrollView中的GridLayout之后:

我需要使用ScrollView中的GridLayout作为Child实现该效果
我的XML布局文件:
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:fillViewport="true">
<LinearLayout
android:id="@+id/boxContainer"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<GridLayout
android:layout_width="wrap_content"
android:layout_height="315dp">
<!-- RelativeLayout children go here -->
</GridLayout>
</LinearLayout>
</ScrollView>
Run Code Online (Sandbox Code Playgroud)
我使用LayoutParams在OnCreate中初始化它们:
//MiddleLayout
GridLayout.LayoutParams middleLayoutLayoutParams = (GridLayout.LayoutParams)middleLayout.getLayoutParams();
middleLayoutLayoutParams.setMargins(2, 273, 400, 0);
middleLayoutLayoutParams.setGravity(Gravity.LEFT);
middleLayoutLayoutParams.width = 424;
middleLayout.setLayoutParams(middleLayoutLayoutParams);
middleLayout.setBackgroundDrawable(new ColorDrawable(Color.RED));
middleLayout.bringToFront();
//MiddleLayout1
GridLayout.LayoutParams middleLayoutLayoutParams1 = (GridLayout.LayoutParams)middleLayout1.getLayoutParams();
middleLayoutLayoutParams1.setMargins(431, 273, -2, 0);
middleLayout1.getLayoutParams().width = 645;
middleLayout1.setLayoutParams(middleLayoutLayoutParams1);
middleLayout1.setBackgroundDrawable(new ColorDrawable(Color.GREEN));
//TopLayout
GridLayout.LayoutParams topLayoutLayoutParams = (GridLayout.LayoutParams)topLayout.getLayoutParams();
topLayoutLayoutParams.setMargins(2, 0, -2, 676);
topLayout.getLayoutParams().width = 631;
topLayout.setLayoutParams(topLayoutLayoutParams);
topLayout.setBackgroundDrawable(new ColorDrawable(Color.rgb(255, 154, 0)));
//TopLayout1
GridLayout.LayoutParams topLayoutLayoutParams1 = …Run Code Online (Sandbox Code Playgroud) 我正在获取一个NullPointerException blah.startAnimation(anim),它位于一个LongClickListener.我想要做的是在DragListener上获取GridLayout中的子项数,并在开始拖动imageview时为所有子项设置动画.由于某种原因,这会返回NullPointerException,
它的来源是:
@Override
public boolean onLongClick(View v) {
View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(v);
v.startDrag(null, shadowBuilder, v, 0);
deleteAreaForAdapter.setVisibility(View.VISIBLE);
deleteAreaForAdapter.startAnimation(slide_in);
for(int i=0; i<middleViewForAdapter.getChildCount(); i++) {
int middleChildCount = middleViewForAdapter.getChildCount();
int middleChildCount1 = middleViewForAdapter.getChildCount();
int topChildCount = middleViewForAdapter.getChildCount();
int topChildCount1 = middleViewForAdapter.getChildCount();
int bottomChildCount = middleViewForAdapter.getChildCount();
LinearLayout topChild = (LinearLayout)topViewForAdapter.getChildAt(i);
LinearLayout topChild1 = (LinearLayout)topViewForAdapter1.getChildAt(i);
LinearLayout bottomChild = (LinearLayout)bottomViewForAdapter.getChildAt(i);
Context context = mContext;
Animation shakeAnim = AnimationUtils.loadAnimation(context, R.anim.shake);
// do stuff with child view
//ll.clearAnimation();
/*middleChild1.startAnimation(shakeAnim);
topChild.startAnimation(shakeAnim);
topChild1.startAnimation(shakeAnim);
bottomChild.startAnimation(shakeAnim);*/ …Run Code Online (Sandbox Code Playgroud) 我怎样才能得到GridLayout的所有孩子?在我的GridLayout中,我正在添加视图blahblah.addView(child),而不是通过XML.GridLayout有点像这样:
+++++++++ ++++++++++ ++++++++++
|ImageView| |ImageView| |ImageView|
| | | | | |
++++++++++ ++++++++++ ++++++++++
[TextView] [Textview] [TextView]
我需要一个按钮来在GridLayout中的所有imageViews上播放动画.别担心,我已经把它覆盖了.所以我需要对child.startAnimation(anim)GridLayout的所有子项做.我怎么能以编程方式执行此操作?
谢谢