我想更改单击的任何按钮的大小

时间:2019-05-31 19:06:14

标签: java android android-button

我使用了9个按钮,并且希望每个人单击的按钮都更大,如果出现任何退出模式,它们将恢复为默认状态。 所有这些按钮都在网格布局中使用 我已经使用了9个按钮,并且想在点击了you can see example View in here

的任何按钮上都更大
  

这是我的源代码

public class MainActivity extends AppCompatActivity implements Animation.AnimationListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        content = 1;


        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);    

//add category
        searchButton = (Button) findViewById(R.id.addCategory);

        searchButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final View promptsView = getLayoutInflater().inflate(R.layout.prompts_category, null);

                AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);
                alertDialogBuilder.setView(promptsView);
                final AlertDialog dialog = alertDialogBuilder.create();

                dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                dialog.show();
                WindowManager.LayoutParams lp = new WindowManager.LayoutParams();

                lp.copyFrom(dialog.getWindow().getAttributes());
                lp.width = 800;
                lp.height = 675;
                dialog.getWindow().setAttributes(lp);

                dialog.getWindow().clearFlags(WindowManager.LayoutParams.FIRST_APPLICATION_WINDOW);

                addCategoryButton = (Button) dialog.findViewById(R.id.addCatButton);
                insertCategoryName = (EditText) dialog.findViewById(R.id.insertCategoryName);

                gridColorsCategory = (GridLayout) dialog.findViewById(R.id.gridColorsCategory);

                setSingleEvent(gridColorsCategory);
                addCategoryButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        insertCategoryName.getText().toString();

                    }
                });
            }
        });


    }

    private void setSingleEvent(GridLayout gridColorsCategory) {
        for (int i = 0; i < gridColorsCategory.getChildCount(); i++) {
            final Button button = (Button) gridColorsCategory.getChildAt(i);
            final int finalI = i;

            final ViewGroup.LayoutParams layoutParams = button.getLayoutParams();

            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                   }
                }
            });
        }

    }

2 个答案:

答案 0 :(得分:0)

您可以使用此方法来调整按钮的大小

private void resizeView(View view, int newWidth, int newHeight) { 
    try { 
        Constructor<? extends LayoutParams> ctor = view.getLayoutParams().getClass().getDeclaredConstructor(int.class, int.class); 
        view.setLayoutParams(ctor.newInstance(newWidth, newHeight));   
    } catch (Exception e) { 
        e.printStackTrace(); 
    }
}

,如果默认值为wrap_content

  private void resizeDefault(View view) { 
        try { 
          ViewGroup.LayoutParams params = textView.getLayoutParams();
          params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
          params.width = ViewGroup.LayoutParams.WRAP_CONTENT;
          view.setLayoutParams(params);   
        } catch (Exception e) { 
            e.printStackTrace(); 
        }
    }

答案 1 :(得分:0)

您可以在可绘制文件夹中创建一个名为btn_go.xml之类的xml文件,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- NOTE: order is important (the first matching state(s) is what is rendered) -->
    <item 
        android:state_pressed="true" 
        android:drawable="@drawable/go_pressed_big” />
    <item 
        android:drawable="@drawable/go_normal_small” />
 </selector>

然后,在视图的layout.xml文件中,您将像这样引用按钮的源代码:

<ImageButton
    android:id="@+id/button_go”
    android:layout_width=“wrap_contents”
    android:layout_height=“wrap_contents”
    android:src="@drawable/btn_go”/>

go_pressed_big图像将比go_normal_small图像稍大,并且通过为宽度和高度指定wrap_contents,按钮应该进行调整。