0

你能帮助我吗?当我按下按钮时,它保持按下状态,但什么也不做。

对不起,我的英语不好!:)

谢谢!

fasmenupincipal.xml 文件:

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

我想从膨胀布局开始一个新的活动。我制作了一个滚动视图,然后将视图添加到滚动视图中,从而使布局膨胀。这是de java代码:

public class ProvaPedidoScroll extends Activity {

    Context mContext;
    HorizontalScrollView mScrollView;
    LinearLayout mLinearLayout;
    LinearLayout.LayoutParams mLinearLayoutParams;
    Display mDisplay;
    // scroll behaviour
    private int mScrollStartPosition;
    private static final float SCROLL_MARGIN = 0.2f;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        // load layout from xml and get references to sub-views
        setContentView(R.layout.scrollview);
        mScrollView = (HorizontalScrollView) findViewById(R.id.scrollview);
        mLinearLayout = (LinearLayout) findViewById(R.id.scrollviewlinearlayout);
        // get a display reference (used to find screen size)
        mDisplay = ((WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
        // get the layout parameters to apply to the sub-views
        mLinearLayoutParams = new LayoutParams(mDisplay.getWidth(), mDisplay.getHeight());
        // add some views to the ScrollView
        addViewsToScrollView();
    }

    /**
     * Inflates and adds some views to the ScrollView
     */
    private void addViewsToScrollView() {
        LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);

        View menuprincipal = inflater.inflate(R.layout.fasmenuprincipal, null);
        menuprincipal.setLayoutParams(mLinearLayoutParams);
        mLinearLayout.addView(menuprincipal);

        Button ClientesMenu = (Button) menuprincipal.findViewById(R.id.button1);

        ClientesMenu.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                int iOpcio = 1;
                Intent VeureClients = new Intent(ProvaPedidoScroll.this, buscar_client.class);
                VeureClients.putExtra("Opcio", iOpcio);
                startActivity(VeureClients);

            }
        });

        View view2 = inflater.inflate(R.layout.fasmenugestion, null);
        view2.setLayoutParams(mLinearLayoutParams);
        mLinearLayout.addView(view2);

    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        int viewWidth = mDisplay.getWidth(); // width of each view
        int triggerWidth = (int) (SCROLL_MARGIN * viewWidth); // amount user has to scroll to move to next view
        int pos = mScrollView.getScrollX();
        int diff = pos % viewWidth; // offset of current scroll from leftmost view's snap position
        int posLeftView = pos - diff; // absolute snap position of the leftmost view on screen
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                // Record the starting scroll position. This is used to decide the scroll direction.
                mScrollStartPosition = pos;
                break;
            case MotionEvent.ACTION_UP:
                if (pos > mScrollStartPosition) {
                    // Scrolling right
                    if (diff > triggerWidth) mScrollView.smoothScrollTo(posLeftView + viewWidth, 0);
                    else mScrollView.smoothScrollTo(posLeftView, 0);
                } else {
                    // Scrolling left
                    if (diff > (viewWidth - triggerWidth)) mScrollView.smoothScrollTo(posLeftView + viewWidth, 0);
                    else mScrollView.smoothScrollTo(posLeftView, 0);
                }
                // replacing our scrollTo command with it's own
                return true;
        }
        return super.dispatchTouchEvent(ev);
    }


}
4

1 回答 1

0

试试这个代码

可能会发现错误

HorizontalScrollView scrollView = (HorizontalScrollView) findViewById(R.id.scrollView1);

        LinearLayout topLinearLayout = new LinearLayout(this);
       // topLinearLayout.setLayoutParams(android.widget.LinearLayout.LayoutParams.FILL_PARENT,android.widget.LinearLayout.LayoutParams.FILL_PARENT);
        topLinearLayout.setOrientation(LinearLayout.HORIZONTAL); 

        for (int i = 0; i < 15; i++){



            final ImageView imageView = new ImageView (this);

            imageView.setTag(i);

            imageView.setImageResource(R.drawable.ic_launcher);

            topLinearLayout.addView(imageView);

            imageView.setOnClickListener(new OnClickListener()
            {

                @Override
                public void onClick(View v)
                {
                    // TODO Auto-generated method stub
                    Log.e("Tag",""+imageView.getTag());
                }
            });


        }

        scrollView.addView(topLinearLayout);
于 2013-02-02T13:07:32.133 回答