在布局内滚动视图

时间:2014-03-12 09:24:22

标签: android

我希望网格布局[带水平滚动]作为子布局说在布局内占据80%的屏幕水平方向,[屏幕的一部分]但是当前运行的屏幕显示全屏网格视图。这是代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@android:color/holo_blue_dark"
    android:orientation="horizontal"
    android:weightSum="10" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="2"
        android:background="@android:color/holo_green_light"
        android:orientation="vertical" >
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="8"
        android:background="@android:color/holo_orange_light"
        android:orientation="vertical"
        android:padding="3dp" >

        <ScrollView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
                  >

            <HorizontalScrollView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >

                <GridLayout
                    android:id="@+id/tint_grid"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:columnCount="48"
                    android:orientation="horizontal"
                    android:rowCount="8" >
                </GridLayout>
            </HorizontalScrollView>
        </ScrollView>
    </LinearLayout>

</LinearLayout>

这是java代码:

package com.example.gridtest3;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.GridLayout;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends Activity {

    int maxRowSize  =   0;
    int maxColSize  =   0;
    int i   =   0;
    int j   =   0;
    int cellcount   =   0;
    ImageView   tintValues[]    =   null;
    GridLayout  tintGrid    =   null;
    int currColCount    =   0;
    int currRowCount    =   0;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        loadGUI();
        handleClicks();


    }

    private int getColCount(int position) {
        currColCount    =   position%maxColSize;
        return currColCount;
        // TODO Auto-generated method stub

    }

    private int getRowCount(int position) {
        currRowCount    =   position/maxColSize;
        return currRowCount;
        // TODO Auto-generated method stub

    }

    private void loadGUI() {
        // TODO Auto-generated method stub
        tintGrid    =   (GridLayout)findViewById(R.id.tint_grid);
        maxRowSize      =   tintGrid.getRowCount();
        maxColSize      =   tintGrid.getColumnCount();
        cellcount   =   maxColSize*maxRowSize;
        tintValues  =   new ImageView[cellcount];




        for(i=0;i<cellcount;i++)
        {
            tintValues[i] = new ImageView(MainActivity.this);
            tintValues[i].setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            tintValues[i].setImageResource(R.drawable.blue);
            tintValues[i].setPadding(2, 2, 2, 2);
            tintGrid.addView(tintValues[i]);

        }
    }

    private void handleClicks() {
        // TODO Auto-generated method stub

        for(j=0;j<cellcount;j++)
        {
            tintValues[j].setOnClickListener(new OnClickListener() {
                int position    =   j;

                int CR  =   getRowCount(position);
                int CC  =   getColCount(position);


                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Toast.makeText(getApplicationContext(),"Tint No. "+position+" Col " +CC+" Row "+CR,Toast.LENGTH_SHORT).show();

                }
            });

        }


    }
}

0 个答案:

没有答案