【问题标题】:How to stretch rows of a grid view in android where gridview contains buttons如何在gridview包含按钮的android中拉伸网格视图的行
【发布时间】:2017-01-27 02:08:48
【问题描述】:

我正在尝试设计一个在 4x4 网格视图中带有按钮的计算器。现在,当我生成视图时,网格下方有空白区域。我希望网格完全填充父级。就像http://rechner-app.com/ 的例子一样。这个怎么做。

activity_main.xml

<TextView
    android:id="@+id/txtStack"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="5sp"
    android:layout_marginRight="5sp"
    android:layout_marginTop="3sp"
    android:gravity="right"
    android:textSize="15sp" />

<TextView
    android:id="@+id/txtInput"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="5sp"
    android:layout_marginRight="5sp"
    android:gravity="right"
    android:textSize="25sp" />

<TextView
    android:id="@+id/txtMemory"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="5sp"
    android:layout_marginRight="5sp"
    android:gravity="left"
    android:textSize="15sp" />

<LinearLayout
    android:id="@+id/gridContainer"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="fill_horizontal"
    android:orientation="vertical" >
    <GridView
        android:id="@+id/grdButtons"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:horizontalSpacing="0.5dp"
        android:numColumns="4"
        android:stretchMode="columnWidth"
        android:verticalSpacing="0.5dp" />
</LinearLayout>

适配器。

public View getView(int position, View convertView, ViewGroup parent) {
    Button btn;
    if (convertView == null) { // if it's not recycled, initialize some
                                // attributes

        btn = new Button(mContext);
        KeypadButton keypadButton = mButtons[position];

        btn.setBackgroundResource(R.drawable.keypad1);

        // Set OnClickListener of the button to mOnButtonClick
        if (keypadButton != KeypadButton.DUMMY)
            btn.setOnClickListener(mOnButtonClick);
        else
            btn.setClickable(false);
        // Set CalculatorButton enumeration as tag of the button so that we
        // will use this information from our main view to identify what to
        // do
        btn.setTag(keypadButton);
        View rootView = LayoutInflater.from(mContext).inflate(R.layout.activity_main, parent, false);
        GridView mKeypadGridContainer = (GridView) rootView.findViewById(R.id.grdButtons);
        Log.d(TAG, mKeypadGridContainer.getHeight() + "sadsd");
        Log.d(TAG, mKeypadGridContainer.getHeight() + " :height");
        //btn.setMinimumHeight(MainActivity.metricsHeight/4);
    } else {
        btn = (Button) convertView;
    }

    btn.setText(mButtons[position].getText());
    return btn;
}

【问题讨论】:

  • 你为什么使用 GridView 而不是 TableLayout?
  • 表格布局是否会自行调整大小。我试试看
  • 恕我直言,我将 GridView 用于“x”数据,其中 x 未知;在您的情况下,您知道组成布局的按钮数量,那么为什么不使用 TableLayout 并使用组件(当然使用 TableRow)。

标签: java android gridview


【解决方案1】:

您将需要一个自定义复合控件。在这里查看我的答案:

How to create a regular, resizable grid without nested weights?

更新

这似乎是一个不错的教程:

http://blog.tomgibara.com/post/1696552527/implement-your-own-android-layouts

您必须根据自己的需要对其进行修改,并且您需要了解 onMeasure 和 onLayout 的工作原理,但是一旦您理解了它,一切就水到渠成,而且很容易。

我有一个项目,我必须构建这样的控件。如果我没记错的话,我首先使用 MeasureSpec 类在父级的 onMeasure() 方法中使用 measure() 方法调用了子级的 onMeasure 方法,然后使用 getMeasuresWidth() 和 -Height 在父级的 onLayout() 中使用了结果() 方法,其中包含测量的尺寸。简而言之就是这样。

【讨论】:

  • 谢谢,但你能给我一个带代码的例子吗?这将非常有帮助。我在想我是否可以使用 xml 来做这些事情。
【解决方案2】:

公共类 ResizeableGridView 扩展 GridView {

private int height;

public ResizeableGridView(Context context) {
    super(context);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
     super.onMeasure(widthMeasureSpec, heightMeasureSpec);
     height = getHeight();
}

@Override
protected void layoutChildren() {
    super.layoutChildren();
    View v = getChildAt(0);
    if((v.getHeight()*5) < height){
        //if the combined height of rows is less than parent then only resize the children to fit parent
        int count = getChildCount();
        for(int i = 0;i < count;i++){
            View view = getChildAt(i);
            ViewGroup.LayoutParams params = view.getLayoutParams();
            //I am using 5 because i used it for calendar and it has exactly five rows
            params.height = height/5;
            view.setLayoutParams(params);
        }//end for
    }//end if
 } // end layoutChildren
} //end GridView

【讨论】:

    【解决方案3】:

    编辑: 必须在您的 xml 中设置列​​数:

    <AutoAdjustableGridView
    android:id="@+id/gridview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:numColumns="3"/>
    

    扩展 GridView。

    将您的 childCount 除以您的列数以获得您的行数。

    将 GridView 的高度除以行数。

    将结果设置为孩子的身高。

    import android.content.Context;
    import android.os.Build;
    import android.support.annotation.RequiresApi;
    import android.util.AttributeSet;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.GridView;
    
    /**
     * by Yuri Kayel on 26/01/2017.
    */
    public class AutoAdjustableGridView extends GridView {
    
    int childCount;
    int columns;
    int rows;
    int gridViewHeight;
    int gridViewWidth;
    ViewGroup.LayoutParams childParams;
    
    public AutoAdjustableGridView(Context context) {
        super(context);
    }
    
    public AutoAdjustableGridView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    
    public AutoAdjustableGridView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public AutoAdjustableGridView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }
    
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        childCount = getChildCount();
        gridViewHeight = getHeight();
        gridViewWidth = getWidth();
        columns = getNumColumns();
        rows = childCount / columns;
    }
    
    @Override
    protected void layoutChildren() {
        super.layoutChildren();
        for (int i = 0; i < childCount; i++) {
            View child = getChildAt(i);
            childParams = child.getLayoutParams();
            childParams.height = gridViewHeight / rows;
            childParams.width = gridViewWidth / columns;
            child.setLayoutParams(childParams);
        }
    }
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-27
      • 1970-01-01
      • 2014-10-28
      • 1970-01-01
      • 2012-02-11
      • 2016-05-12
      • 2011-10-27
      • 2016-01-06
      相关资源
      最近更新 更多