以编程方式添加项目-Android

亚历山大·C·杜卡莫

我在xml中创建了该项目:

<com.example.alexandre_pc.beerin.RoundedImageView
    android:id="@+id/iconViewUser"
    android:layout_width="40dp"
    android:layout_height="40dp"
    android:scaleType="fitXY"
    android:src="@mipmap/user"
/>

我需要以编程方式创建此项目并将其添加到中LinearLayout在我的活动中,我创建了一个LinearLayout

 LinearLayout linear = new LinearLayout(this);
 linear.setOrientation(LinearLayout.VERTICAL);
 LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(40, 90);
 layoutParams.setMargins(0,0,5,0);
 linear.addView(item, layoutParams);

但是我不知道如何以编程方式创建它。我该怎么做?

极乐
import com.example.alexandre_pc.beerin.RoundedImageView;

在onCreate()中

//create your View

    RoundedImageView yourName = new RoundedImageView();

//set LayoutParameters

    RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) yourName.getLayoutParams();
            layoutParams.height = yourHeight;
            layoutParams.width = yourWidth;
    yourName.setLayoutParams(layoutParams);

//set scaleType

     yourName.setScaleType(ImageView.ScaleType.ANY_SCALE_TYPE);
//setResource
     yourName.setImageResource(R.drawable.yourDrawable);

然后,像您最初所做的那样添加新创建的视图

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Android:以编程方式添加TextInputLayout

如何以编程方式从Android通过mainActivity将项目添加到arrayList中的stringArray

Android-以编程方式选择GridView的项目?

Android添加<item>以编程方式设置样式

Android以编程方式添加的ListPreference缺少标题

Android:以编程方式添加TextView无效

Android:以编程方式添加ImageView时崩溃?

在android中以编程方式添加列表项

Android通过ConstraintLayout以编程方式添加ImageView

Android:在屏幕底部以编程方式添加视图

在Android LinearLayout中以编程方式添加图标

在Android中以编程方式将动画效果添加到(以编程方式添加)popupWindow

在RecycerView Android中以编程方式为项目设置重力

Android自定义Listview以编程方式单击项目

Android以编程方式在FrameLayout中添加按钮

Android以编程方式向工具栏添加按钮

在Android中以编程方式添加LinearLayout不起作用

使用Android颜色资源以编程方式添加透明度

Android以编程方式向Webview添加边距

Android:以编程方式添加的布局会忽略主题

以编程方式向Android中的framelayout添加片段

Android以编程方式添加视图而不监听属性

Android以编程方式添加布局资源以进行查看

Android Studio以编程方式添加scrollview无法滚动

如何以编程方式在Android布局中添加新按钮

Android:以编程方式向通知添加按钮

以编程方式向Android Button添加保证金

Android:以编程方式添加多个按钮的错误

Android:以编程方式向图片添加红色箭头