开发者

Giving right margin in button on being clicked in android

I have a screen where I display f开发者_JS百科ive buttons inside a Linearlayout vertically. this linear layout is inside a relative layout and aligned to parent right. Now I need that when any button is clicked it should shift leftwards while others remain at the original position. I am trying to give left margin to the button on being clicked but the problem is all the buttons are shifting and not only that one.

Here is the code

 final LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
             LinearLayout.LayoutParams.WRAP_CONTENT,     LinearLayout.LayoutParams.WRAP_CONTENT);
    layoutParams.rightMargin = 20;

    mColorButton.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v) {
            fillDataList(1);
            mIsClicked = true;
            //[start] setting the colour of button
                    mColorButton.setBackgroundResource(R.drawable.yellow_stick) ;
                mColorButton.setLayoutParams(layoutParams);
                    mPatternButton.setBackgroundResource(R.drawable.brown_stick);
                    mDistributionButton.setBackgroundResource(R.drawable.brown_stick);
                    mShapeButton.setBackgroundResource(R.drawable.brown_stick);
                    mLesionButton.setBackgroundResource(R.drawable.brown_stick);
            //[end] setting the colour of button
        }           
    });


May be this is the problem:

By default all Buttons are left aligned and when you give right margin to a Button it expands its parent layout size. The parent expands it self with its child. So all the Button shifted left side.

try to give android:layout_gravity="right" to all Button in XML or code.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜