我在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] 删除。
我来说两句