有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

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

this is my source code

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) 个答案

  1. # 1 楼答案

    您可以在可绘制文件夹中创建一个xml文件,名为btn_go。如下所示的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>
    

    然后,在视图的布局中。xml文件,您可以引用按钮的源代码,如下所示:

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

    go_pressed_大图像将略大于go_normal_小图像,通过为宽度和高度指定wrap_内容,按钮应进行调整

  2. # 2 楼答案

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

    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(); 
            }
        }