java – 将按钮高度和宽度设置为包装内容并填充父级

前端之家收集整理的这篇文章主要介绍了java – 将按钮高度和宽度设置为包装内容并填充父级前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在开发一个应用程序,用户应该可以通过按其他按钮来改变按钮的外观.我使用四个按钮将高度设置为换行内容,将高度设置为填充父级,将宽度设置为换行内容,将宽度设置为填充父级.

我用Google搜索了一下,并找到了使用LayoutParams的解决方案,尽管该代码没有指定是否更改了宽度的高度.我也有错误说我的IDE无法识别“LayoutParams”.做这个的最好方式是什么?

解决方法

你需要寻找的是View.ViewGroup.LayoutParams.
每个LayoutParams都具有MATCH_PARENT和WRAP_CONTENT属性的常量值.
我已准备好您可以使用的简单代码示例:

活动:

  1. package com.example.stack2;
  2.  
  3. import android.app.Activity;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.view.View.OnClickListener;
  7. import android.view.ViewGroup.LayoutParams;
  8. import android.widget.Button;
  9.  
  10. public class MainActivity extends Activity implements OnClickListener{
  11.  
  12. Button test;
  13. @Override
  14. public void onCreate(Bundle savedInstanceState) {
  15. super.onCreate(savedInstanceState);
  16. setContentView(R.layout.activity_main);
  17. Button b = (Button)findViewById(R.id.button1);
  18. b.setOnClickListener(this);
  19. b = (Button)findViewById(R.id.button2);
  20. b.setOnClickListener(this);
  21. b = (Button)findViewById(R.id.button3);
  22. b.setOnClickListener(this);
  23. b = (Button)findViewById(R.id.button4);
  24. b.setOnClickListener(this);
  25. test = (Button)findViewById(R.id.test);
  26. }
  27. public void onClick(View v)
  28. {
  29. LayoutParams lp = test.getLayoutParams();
  30. if(v.getId() == R.id.button1)
  31. {
  32. lp.height = LayoutParams.WRAP_CONTENT;
  33. }else if(v.getId() == R.id.button2){
  34. lp.width = LayoutParams.WRAP_CONTENT;
  35. }else if(v.getId() == R.id.button3){
  36. lp.height = LayoutParams.MATCH_PARENT;
  37. }else if(v.getId() == R.id.button4){
  38. lp.width = LayoutParams.MATCH_PARENT;
  39. }
  40. test.setLayoutParams(lp);
  41. }
  42. }

布局xml:

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical" >
  6.  
  7. <LinearLayout
  8. android:layout_height="wrap_content"
  9. android:layout_width="match_parent"
  10. android:orientation="horizontal">
  11.  
  12. <Button
  13. android:id="@+id/button1"
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"
  16. android:text="h_wc" />
  17.  
  18. <Button
  19. android:id="@+id/button2"
  20. android:layout_width="wrap_content"
  21. android:layout_height="wrap_content"
  22. android:text="w_wc" />
  23.  
  24. <Button
  25. android:id="@+id/button3"
  26. android:layout_width="wrap_content"
  27. android:layout_height="wrap_content"
  28. android:text="h_fp" />
  29.  
  30. <Button
  31. android:id="@+id/button4"
  32. android:layout_width="wrap_content"
  33. android:layout_height="wrap_content"
  34. android:text="h_fp" />
  35.  
  36. </LinearLayout>
  37.  
  38. <Button
  39. android:id="@+id/test"
  40. android:layout_width="wrap_content"
  41. android:layout_height="wrap_content"
  42. android:text="test" />
  43.  
  44. </LinearLayout>

猜你在找的Java相关文章