【问题标题】:I want to change the size of any button clicked我想更改单击的任何按钮的大小
【发布时间】:2019-05-31 19:06:14
【问题描述】:

我已经使用了 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) {

                   }
                }
            });
        }

    }

【问题讨论】:

    标签: java android android-button


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      您可以在您的可绘制文件夹中创建一个名为 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 的宽度和高度,按钮应该进行调整。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-01-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多