【问题标题】:How to use weights to arrange Gridview Items programmatically with TableLayout如何使用权重通过 TableLayout 以编程方式排列 Gridview 项目
【发布时间】:2013-04-29 15:54:18
【问题描述】:

我正在开发 Suduko 求解器应用程序。我希望布局在 9 个 Gridview 中有 9x9 TextView。我想在它们下面放两个按钮(解决,清除)。我已经做到了,但它可能不适用于小型设备(看不到按钮,并且网格视图变成了不适合屏幕的可滚动视图)

这是我如何将 Textviews 添加到 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

<?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>

这是我的截图

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

【问题讨论】:

    标签: android android-layout gridview android-tablelayout


    【解决方案1】:

    这里有个技巧:对于一个完全统一的表格或网格,您根本不需要 TableView 或 GridView。只需将所有内容放入 LinearLayout 中,size=0 和 layoutWeight=1。 size=0 似乎违反直觉,但结合 layoutWeight=1,它具有使所有子小部件的大小完全相同的效果,而不管它们的内容如何。

    所以做这样的事情:

    <!-- 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>
    

    【讨论】:

    • 注意:我把它留作练习,让学生弄清楚如何在你想要的地方添加页边距。
    • 好的,但是我应该如何将 TextView 添加到 .java 文件中的每个视图(以编程方式)?
    • LinearLayout 不处理缓存,如果您将太多视图放入其中,足以耗尽内存,应用程序将崩溃。但在 gridview 或 listview 中,这不会发生。
    【解决方案2】:

    检查这个数独项目 -

    Git Hub

    还有

    Check This

    在这里您可以找到数独的布局和源代码以及其他有用的东西。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-29
      • 2012-02-23
      • 1970-01-01
      • 2010-12-04
      相关资源
      最近更新 更多