为什么GridLayout下面会多出一片空白区域?并且还遮盖了我的一个视图

「已注销」 2016-08-08 10:22:40
我是仿制的2048.
这是运行后的效果图:
下面多出了一截空白没有格子的区域,格子我是动态添加的,稍后会把代码的关键片段截图贴上来。



这是我的布局文件有关于GridLayout的关键布局代码(该项目只有一个布局文件):
该布局文件整体是用的LinearLayout布局方式,然后你们看到截图中的com.......GameView就是在代码部分继承了GridLayout,然后它下面的一个RelativeLayout就是我想放在屏幕底部的两个button的布局。



这是GameView.class的关键代码片段截图:
它继承了GridLayout。第一个方法的目的是针对使用不同的手机适配不同的屏幕,这个GridLayout的宽高会发生变化,所以得在宽高发生变化的时候动态的改变视图中的card的宽高;第二个方法的目的是添加一个4*4的card。



这是Card.class的关键代码片段截图:
它继承了FramLayout。在构造方法中初始化这个card,它是一个TextView控件,在里面配置它的属性。


最后再强调一下我的问题:
为什么GridLayout下面会多出了一截空区域,怎么去掉(我尝试过修改GameView的布局高度为wrap_content,也不行,整个视图会变形。)并且这个GameView还遮住了我想放在底部的两个按钮。
...全文
507 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
tudouzi007 2016-08-08
  • 打赏
  • 举报
回复
我试了下 因为你给的代码有的类没有提供 所以你自定义的控件我没法试 我就用了最原始的gridview来试的
我把gridview高度调成了wrap_content 亲测好使 没毛病
这是代码(变动的地方我用红色标出):
xml布局:
<LinearLayout 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:orientation="vertical"
tools:context="${relativePackage}.${activityClass}" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50px"
android:orientation="horizontal" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2048"
android:textSize="60dp" />

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10px"
android:layout_marginTop="10px"
android:background="@drawable/abc_menu_hardkey_panel_mtrl_mult"
android:orientation="vertical" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:padding="5dp"
android:text="score"
android:textSize="20dp" />

<TextView
android:id="@+id/tvScore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="5dp"
android:text="0"
android:textColor="#7F2026"
android:textSize="20dp"
android:textStyle="bold" >
</TextView>
</LinearLayout>

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50px"
android:layout_marginTop="10px"
android:background="@drawable/abc_menu_hardkey_panel_mtrl_mult"
android:orientation="vertical" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:padding="5dp"
android:text="bestScores"
android:textSize="20dp" />

<TextView
android:id="@+id/tvBestScore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="5dp"
android:text="0"
android:textColor="#7F2026"
android:textSize="20dp"
android:textStyle="bold" >
</TextView>
</LinearLayout>
</LinearLayout>

<GridView
android:id="@+id/grid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numColumns="4" />


<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >

<Button
android:id="@+id/btnRestart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_margin="10dp"
android:layout_marginLeft="50dp"
android:background="#FFF0E1"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:text="Restart" >
</Button>

<Button
android:id="@+id/btnOptions"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_margin="10dp"
android:layout_marginRight="50dp"
android:background="#FFF0E1"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:text="Options" >
</Button>
</RelativeLayout>

</LinearLayout>
............................................................................................................................................
MainActivity:
public class MainActivity extends Activity {
GridView gridView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridView = (GridView) findViewById(R.id.grid);
gridView.setAdapter(new MyAdapter(this));
}
}
............................................................................................................................................................
adapter:
public class MyAdapter extends BaseAdapter {
Context context;

public MyAdapter(Context context) {
this.context = context;
}

@Override
public int getCount() {
return 16;
}

@Override
public Object getItem(int position) {
return position;
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView==null) {
convertView=((Activity)context).getLayoutInflater().inflate(R.layout.item, null);
}
TextView textView=(TextView) convertView.findViewById(R.id.textview);
textView.setText("测试"+position);
return convertView;
}
}
........................................................................................................................................................................
gridview的子项布局:
<?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:id="@+id/textview"
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_margin="10px"
android:background="#ffffff" />

</LinearLayout>
........................................................................................................................................................
效果图:

tudouzi007 2016-08-08
  • 打赏
  • 举报
回复
gridview的适配器adapter也没有
tudouzi007 2016-08-08
  • 打赏
  • 举报
回复
是不是还缺个Card的类啊
「已注销」 2016-08-08
  • 打赏
  • 举报
回复
GameView类: [code=java]package com.example.myapplication; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.graphics.Point; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; import android.widget.GridLayout; import java.util.ArrayList; import java.util.List; /** * Created by Cheng on 2016/7/27. */ public class GameView extends GridLayout { private Card[][] cardMap = new Card[4][4]; private List<Point> emptyPoints = new ArrayList<>(); public GameView(Context context) { super(context); initGameView(); } public GameView(Context context, AttributeSet attrs) { super(context, attrs); initGameView(); } public GameView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initGameView(); } private void initGameView() { setBackgroundColor(0xffbbada0); setOnTouchListener(new OnTouchListener() { private float startX, startY, offsetX, offsetY; @Override public boolean onTouch(View view, MotionEvent motionEvent) { switch (motionEvent.getAction()) { case MotionEvent.ACTION_DOWN: startX = motionEvent.getX(); startY = motionEvent.getY(); break; case MotionEvent.ACTION_UP: offsetX = motionEvent.getX() - startX; offsetY = motionEvent.getY() - startY; if (Math.abs(offsetX) > Math.abs(offsetY)) { if (offsetX < -70) { swipeLeft(); } else if (offsetX > 70) { swipeRight(); } } else { if (offsetY < -70) { swipeUp(); } else if (offsetY > 70) { swipeDown(); } } break; } return true; } }); } private void startGame() { MainActivity.getMainActivity().clearScore(); for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) { cardMap[x][y].setNum(0); } } addRandomNum(); addRandomNum(); } private void addRandomNum() { emptyPoints.clear(); for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) { if (cardMap[x][y].getNum() <= 0) { emptyPoints.add(new Point(x, y)); } } } Point p = emptyPoints.remove((int) (Math.random() * emptyPoints.size())); cardMap[p.x][p.y].setNum(Math.random() > 0.1 ? 2 : 4); } private void swipeUp() { boolean merge = false; for (int x = 0; x < 4; x++) { for (int y = 0; y < 4; y++) { for (int y1 = y + 1; y1 < 4; y1++) { if (cardMap[x][y1].getNum() > 0) { if (cardMap[x][y].getNum() <= 0) { cardMap[x][y].setNum(cardMap[x][y1].getNum()); cardMap[x][y1].setNum(0); y--; merge = true; } else if (cardMap[x][y].equals(cardMap[x][y1])) { cardMap[x][y].setNum(cardMap[x][y].getNum() * 2); cardMap[x][y1].setNum(0); MainActivity.getMainActivity().addScore(cardMap[x][y].getNum()); merge = true; } break; } } } } if (merge) { addRandomNum(); checkComplete(); } } private void swipeDown() { boolean merge = false; for (int x = 0; x < 4; x++) { for (int y = 3; y >= 0; y--) { for (int y1 = y - 1; y1 >= 0; y1--) { if (cardMap[x][y1].getNum() > 0) { if (cardMap[x][y].getNum() <= 0) { cardMap[x][y].setNum(cardMap[x][y1].getNum()); cardMap[x][y1].setNum(0); y++; merge = true; } else if (cardMap[x][y].equals(cardMap[x][y1])) { cardMap[x][y].setNum(cardMap[x][y].getNum() * 2); cardMap[x][y1].setNum(0); MainActivity.getMainActivity().addScore(cardMap[x][y].getNum()); merge = true; } break; } } } } if (merge) { addRandomNum(); checkComplete(); } } private void swipeLeft() { boolean merge = false; for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) { for (int x1 = x + 1; x1 < 4; x1++) { if (cardMap[x1][y].getNum() > 0) { if (cardMap[x][y].getNum() <= 0) { cardMap[x][y].setNum(cardMap[x1][y].getNum()); cardMap[x1][y].setNum(0); x--; merge = true; } else if (cardMap[x][y].equals(cardMap[x1][y])) { cardMap[x][y].setNum(cardMap[x][y].getNum() * 2); cardMap[x1][y].setNum(0); MainActivity.getMainActivity().addScore(cardMap[x][y].getNum()); merge = true; } break; } } } } if (merge) { addRandomNum(); checkComplete(); } } private void swipeRight() { boolean merge = false; for (int y = 0; y < 4; y++) { for (int x = 3; x >= 0; x--) { for (int x1 = x - 1; x1 >= 0; x1--) { if (cardMap[x1][y].getNum() > 0) { if (cardMap[x][y].getNum() <= 0) { cardMap[x][y].setNum(cardMap[x1][y].getNum()); cardMap[x1][y].setNum(0); x++; merge = true; } else if (cardMap[x][y].equals(cardMap[x1][y])) { cardMap[x][y].setNum(cardMap[x][y].getNum() * 2); cardMap[x1][y].setNum(0); MainActivity.getMainActivity().addScore(cardMap[x][y].getNum()); merge = true; } break; } } } } if (merge) { addRandomNum(); checkComplete(); } } private void checkComplete() { boolean complete = true; ALL: for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) { if (cardMap[x][y].getNum() == 0 || (x>0 && cardMap[x][y].equals(cardMap[x-1][y])) || (x<3 && cardMap[x][y].equals(cardMap[x+1][y])) || (y>0 && cardMap[x][y].equals(cardMap[x][y-1])) || (y<3 && cardMap[x][y].equals(cardMap[x][y+1]))) { complete = false; break ALL; } } } if (complete) { new AlertDialog.Builder(getContext()).setTitle("你好").setMessage("游戏结束").setPositiveButton("重来", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { startGame(); } }).show(); } } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); int cardWidth = (Math.min(w, h) - 10) / 4; addCard(cardWidth, cardWidth); startGame(); } private void addCard(int cardWidth, int cardHeight) { Card c; for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) { c = new Card(getContext()); c.setNum(0); addView(c, cardWidth, cardHeight); cardMap[x][y] = c; } } } }
「已注销」 2016-08-08
  • 打赏
  • 举报
回复
依照要求我把项目的全部代码和布局代码贴上来。(另外我在思考,这个问题可以不可以换一种思路,GridLayout的高度其实本身就不应该用match,而应该用wrap,所以要解决的应该是wrap的高度导致视图变形的问题)下面是代码: 布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    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:orientation="vertical"
    tools:context="com.example.myapplication.MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="50px"
        android:orientation="horizontal"
        >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_marginBottom="50px"
            android:layout_marginLeft="30px"
            android:layout_marginRight="50px"
            android:text="2048"
            android:textSize="60dp"
            />

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10px"
            android:layout_marginTop="10px"
            android:background="@drawable/abc_menu_hardkey_panel_mtrl_mult"
            android:orientation="vertical">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:padding="5dp"
                android:text="@string/score"
                android:textSize="20dp"/>

            <TextView
                android:id="@+id/tvScore"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="5dp"
                android:text="0"
                android:textColor="#7F2026"
                android:textSize="20dp"
                android:textStyle="bold">
            </TextView>
        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="50px"
            android:layout_marginTop="10px"
            android:background="@drawable/abc_menu_hardkey_panel_mtrl_mult"
            android:orientation="vertical">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:padding="5dp"
                android:text="@string/bestScores"
                android:textSize="20dp"/>

            <TextView
                android:id="@+id/tvBestScore"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="5dp"
                android:text="0"
                android:textColor="#7F2026"
                android:textSize="20dp"
                android:textStyle="bold">
            </TextView>
        </LinearLayout>

    </LinearLayout>

    <com.example.myapplication.GameView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:columnCount="4"
        />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >

        <Button
            android:id="@+id/btnRestart"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_margin="10dp"
            android:layout_marginLeft="50dp"
            android:background="#FFF0E1"
            android:paddingLeft="20dp"
            android:paddingRight="20dp"
            android:text="Restart"
            >

        </Button>

        <Button
            android:id="@+id/btnOptions"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_margin="10dp"
            android:layout_marginRight="50dp"
            android:background="#FFF0E1"
            android:paddingLeft="20dp"
            android:paddingRight="20dp"
            android:text="Options"

            >

        </Button>

    </RelativeLayout>


</LinearLayout>
「已注销」 2016-08-08
  • 打赏
  • 举报
回复
引用 3 楼 tudouzi007 的回复:
完整的代码贴出来 不要片段
请稍等
tudouzi007 2016-08-08
  • 打赏
  • 举报
回复
完整的代码贴出来 不要片段
「已注销」 2016-08-08
  • 打赏
  • 举报
回复
引用 1 楼 tudouzi007 的回复:
你最外层是垂直的线性布局,从上往下布局,当布到GameView的时候,你发没发现他的高度是match_parent,你已经叫他填充满除了上面LinearLayout以外所有空间了,你底下的RelativeLayout当然就不会出现在屏幕里了,你可以把GameView高度改成warp_content试试,应该能解决了
你的想法,我尝试过的。不知道你有没有注意到我帖子的最后一段,我也注明了。改成wrap_content GridLayout就变形了。
tudouzi007 2016-08-08
  • 打赏
  • 举报
回复
你最外层是垂直的线性布局,从上往下布局,当布到GameView的时候,你发没发现他的高度是match_parent,你已经叫他填充满除了上面LinearLayout以外所有空间了,你底下的RelativeLayout当然就不会出现在屏幕里了,你可以把GameView高度改成warp_content试试,应该能解决了

80,469

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧