【问题标题】:[android]How to implement Jess Ander's TwoWayGridView?[android]Jess Ander的TwoWayGridView如何实现?
【发布时间】:2013-09-29 06:29:02
【问题描述】:

我正在尝试在单击按钮时动态添加图像按钮。当图像按钮的数量超过屏幕宽度时,我应该能够水平滚动。我试图实现Jess-Ander's TwoWayGridView 但没有成功。我是初学者。所以如果错误太初级,请多多包涵。

<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=".MainActivity" >

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="onClick"
    android:text="Button" />

<com.jess.ui.TwoWayGridView

xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#E8E8E8"
android:id="@+id/gridview"
android:layout_width="fill_parent" 
android:layout_height="fill_parent"
app:cacheColorHint="#E8E8E8"
app:columnWidth="80dp"
app:rowHeight="80dp"
app:numColumns="auto_fit"
app:numRows="2"
app:verticalSpacing="16dp"
app:horizontalSpacing="16dp"
app:stretchMode="spacingWidthUniform"
app:scrollDirectionPortrait="vertical"
app:scrollDirectionLandscape="horizontal"
app:gravity="center">

<LinearLayout
    android:id="@+id/linearLayout1"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
</com.jess.ui.TwoWayGridView>

</LinearLayout>

这是代码:

package com.example.dynamic;


import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;


public class MainActivity extends Activity {

    LinearLayout linearLayout1;




@Override
public void onCreate(Bundle bundle) {
    super.onCreate(bundle);
    setContentView(R.layout.activity_main);
    linearLayout1 = (LinearLayout) findViewById(R.id.linearLayout1);



}

public void onClick(View v){

    ImageView image = new ImageView(MainActivity.this);
    image.setBackgroundResource(R.drawable.ic_launcher);
    linearLayout1.addView(image);        

}



}

【问题讨论】:

    标签: android gridview horizontalscrollview


    【解决方案1】:

    如果您在使用 xml 中的值获取正确间距时遇到问题,可以尝试查看以下教程:

    http://spragucm.wordpress.com/2013/11/17/android-horizontal-and-vertical-gridview-tutorial/

    我专门写了它,因为双向网格视图项目不会均匀分布,它们不会填满行/列。我的教程中的示例代码允许您设置列号和行号,其他所有事情都为您完成,以便孩子用项目之间的一些填充来填充行/列。

    示例代码演示了如何在任一方向使用双向网格视图。

    至于设置 onClickListener...您需要在 gridview 上设置 OnItemClickListener()。

    【讨论】:

      【解决方案2】:

      我一直在捣鼓实现双向gridview,但最终以我的方式实现了它:

      <ScrollView
                      android:id="@+id/mainscrolView"
                      android:layout_width="match_parent"
                      android:layout_height="match_parent"
                      android:orientation="vertical"
                      >
      
      
                      <HorizontalScrollView
                          android:id="@+id/rclv"
                          android:layout_width="match_parent"
                          android:layout_height="wrap_content"
                          >
      
      
                              <TableLayout
                                  android:id="@+id/table"
                                  android:layout_width="wrap_content"
                                  android:layout_height="wrap_content"
                                  android:stretchColumns="*"
                                  >
      
                              </TableLayout>
      
                      </HorizontalScrollView>
      </ScrollView>
      

      我以编程方式添加 TableLayout 的子项。喜欢:

      TableLayout tv =  findViewById(R.id.table);
       tv.removeAllViewsInLayout();
      
       TableRow tr = new TableRow(AndroidDatabaseManager.this);
      
       tr.setLayoutParams(new LayoutParams(
                              LayoutParams.MATCH_PARENT,
                              LayoutParams.WRAP_CONTENT));
       TextView b3 = new TextView(AndroidDatabaseManager.this);
      b3.setText("Child");
      tr.addView(b3);
      
       tv.addView(tr);
      
                      final View vline = new View(AndroidDatabaseManager.this);
                      vline.setLayoutParams(new
                              TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,2));
                      vline.setBackgroundColor(Color.BLUE);
                      tv.addView(vline); 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-12-09
        • 1970-01-01
        • 1970-01-01
        • 2013-03-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多