【问题标题】:Add an ImageButton programmatically with a Chosen Size以编程方式添加具有所选大小的 ImageButton
【发布时间】:2017-12-10 20:49:51
【问题描述】:

我在 Android Studio 上,我想在我的代码中创建一个 ImageButton。问题是,当我创建它时,我不知道如何设置它的大小(太大了)。

我是这样进行的:我有一个使用 xml(添加)创建的线性布局,我在其中放置了一个包含 TextView 和我的 ImageButton 的 RelativeLayout。当我将 IB 放入布局中时,我设置了参数。这是代码:

final RelativeLayout rl = new RelativeLayout(Selection.this);
final TextView someone = new TextView(Selection.this);
final ImageButton cross = new ImageButton(Selection.this);

RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);

cross.setBackgroundResource(R.drawable.stop);

RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams( 
   RelativeLayout.LayoutParams.WRAP_CONTENT, 
   RelativeLayout.LayoutParams.WRAP_CONTENT);

cross.setLayoutParams(lp);
cross.setScaleType(ImageView.ScaleType.FIT_XY);

someone.setText(et.getText());

rl.addView(someone);
rl.addView(cross);
added.addView(rl,rlp);

【问题讨论】:

    标签: android imagebutton android-imagebutton


    【解决方案1】:

    ImageButton 获取的参数是 lp 的参数,您设置了宽度和高度 RelativeLayout.LayoutParams.WRAP_CONTENT ,因此 ImageButton 将使用可能对您来说太大的图像大小。尝试选择特定尺寸:

    RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams( 
    60,60);
    

    或将 ImageButton 视图添加到您已经限制其大小的另一个布局中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-30
      • 1970-01-01
      • 2016-05-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-04
      • 1970-01-01
      相关资源
      最近更新 更多