如何使用权重与TableLayout编程安排的GridView项目 [英] How to use weights to arrange Gridview Items programmatically with TableLayout

查看:248
本文介绍了如何使用权重与TableLayout编程安排的GridView项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个Suduko求解器应用程序。我想布局有9x9的TextView的9 GridView的。我希望把它们下面两个按钮(解决,清除)。我做了它,但它不与小型设备可能会工作(不能老是看到按钮和网格视图转向滚动不适合屏幕)

I am working on a Suduko solver app. I want the layout to have 9x9 TextView in 9 Gridviews. I want to put below them two buttons (Solve, clear). I have made it but it does not work probably with small devices (Can`t see the buttons and the Grid view turned to scrollable that does not fit the screen)

下面是我如何添加Textviews到GridView

Here is how I add Textviews to the Gridview

gridView = (GridView) findViewById(R.id.gridView1);
String[] arrayEmpty = new String[] {"", "", "", "", "", "", "", "", ""};
ArrayList<String> listEmpty = new ArrayList<String>(Arrays.asList(arrayEmpty));
gridView.setAdapter(new ArrayAdapter<String>(this,R.layout.list_item,list));

下面是我的Layout.XML

Here is my Layout.XML

<?xml version="1.0" encoding="utf-8"?>

<TableRow android:id="@+id/tableRow1"
    android:layout_width="match_parent"
    android:background="#999999"
    android:layout_weight="1"
    android:layout_height="match_parent">

    <GridView
    android:id="@+id/gridView1"
    android:clickable="true"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="2dp"
    android:background="#5C5C5C"
    android:layout_weight="1"
    android:numColumns="3" >
    </GridView>

    <GridView
    android:id="@+id/gridView2"
    android:clickable="true"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="2dp"
    android:background="#5C5C5C"
    android:layout_weight="1"
    android:numColumns="3" >
</GridView>

<GridView
    android:id="@+id/gridView3"
    android:clickable="true"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="2dp"
    android:background="#5C5C5C"
    android:layout_weight="1"
    android:numColumns="3" >
</GridView>

<TableRow android:id="@+id/tableRow2"
    android:layout_width="match_parent"
    android:background="#999999"
    android:layout_weight="1"
    android:layout_height="match_parent">

    <GridView
    android:id="@+id/gridView4"
    android:clickable="true"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="2dp"
    android:background="#5C5C5C"
    android:layout_weight="1"
    android:numColumns="3" >
    </GridView>

    <GridView
    android:id="@+id/gridView5"
    android:clickable="true"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="2dp"
    android:background="#5C5C5C"
    android:layout_weight="1"
    android:numColumns="3" >
</GridView>

<GridView
    android:id="@+id/gridView6"
    android:clickable="true"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="2dp"
    android:background="#5C5C5C"
    android:layout_weight="1"
    android:numColumns="3" >
</GridView>

<TableRow android:id="@+id/tableRow3"
    android:layout_width="match_parent"
    android:background="#999999"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:layout_marginBottom="15dp">

    <GridView
    android:id="@+id/gridView7"
    android:clickable="true"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="2dp"
    android:background="#5C5C5C"
    android:layout_weight="1"
    android:numColumns="3" >
    </GridView>

    <GridView
    android:id="@+id/gridView8"
    android:clickable="true"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="2dp"
    android:background="#5C5C5C"
    android:layout_weight="1"
    android:numColumns="3" >
</GridView>

<GridView
    android:id="@+id/gridView9"
    android:clickable="true"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="2dp"
    android:background="#5C5C5C"
    android:layout_weight="1"
    android:numColumns="3" >
</GridView>

<TableRow android:layout_weight="1" android:layout_height="match_parent">
    <Button
        android:id="@+id/solve_button"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="5dp"
        android:layout_weight="1"
        android:text="@string/solve" />
    <Button
        android:id="@+id/clear_button"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="5dp"
        android:layout_weight="1"
        android:text="@string/_clear" />
</TableRow>

这是什么,我有一个截图

And this is a screenshot of what I had

但是,这是我需要我的布局要与所有设备。也许我应该用权重,但我不`吨知道如何

But this is what I need my Layout to be with all Devices. Maybe I should use weights, but I don`t know how

推荐答案

下面是一招:对于一个完全统一的表格或网格,你不需要的TableView或GridView控件的。只要把里面的东西用的LinearLayout大小= 0和layoutWeight = 1。该大小= 0似乎违反直觉,但与layoutWeight = 1相结合,它具有使所有子部件完全相同的大小,而不管它们的内容的效果。

Here's a trick: For a completely uniform table or grid, you don't need TableView or GridView at all. Just put everything inside LinearLayout with size=0 and layoutWeight=1. That size=0 seems counter-intuitive, but combined with the layoutWeight=1, it has the effect of giving all child widgets the exact same size, regardless of their contents.

所以,做这样的事情:

<!-- Outer container; a vertical layout that fills the screen -->
<LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
  <!-- first row -->
  <LinearLayout
      android:orientation="horizontal"
      android:layout_width="fill_parent"
      android:layout_height="0dp"
      android:layout_weight="1">
    <!-- contents of the first row -->
    <!-- first item in first row -->
    <View
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1">
    <!-- second item in first row -->
    <View
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1">
    <!-- third item in first row -->
    <View
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1">
  </LinearLayout>
  <!-- second row -->
  <LinearLayout
      android:orientation="horizontal"
      android:layout_width="fill_parent"
      android:layout_height="0dp"
      android:layout_weight="1">
    <!-- contents of the second row. -->
    <!-- etc -->
  </LinearLayout>
  <!-- third row -->
  <LinearLayout
      android:orientation="horizontal"
      android:layout_width="fill_parent"
      android:layout_height="0dp"
      android:layout_weight="1">
    <!-- contents of the third row -->
    <!-- etc -->
  </LinearLayout>
  <Button android:id="@+id/solveButton
      android:layout_width="fill_parent"
      android:layout_width="wrap_content" />
  <Button android:id="@+id/clearButton
      android:layout_width="fill_parent"
      android:layout_width="wrap_content" />
</LinearLayout>

这篇关于如何使用权重与TableLayout编程安排的GridView项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆