[英]I want to change the size of any button clicked
我已經使用了9個按鈕,我希望在點擊的任何人身上做得更大,如果出現任何退出模式,他們將返回默認狀態。 所有這些按鈕都用在網格布局中我使用了9個按鈕,我希望在點擊的任何人身上做得更大你可以看到示例在這里查看
這是我的源代碼
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) {
}
}
});
}
}
您可以使用此方法調整按鈕的大小
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();
}
}
您可以在drawable文件夾中創建一個名為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圖像,並且通過為width和height指定wrap_contents,按鈕應該調整。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.