80,492
社区成员
发帖
与我相关
我的任务
分享


<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/content_info_add"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="org.bobbit.com.bobbitqrscan.ui.InfoAddActivity"
tools:showIn="@layout/activity_info_add">
<LinearLayout
android:id="@+id/ll_info_add"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingTop="@dimen/activity_vertical_margin"
tools:ignore="ScrollViewSize">
<!--以下部分为需要动态创建的部分-->
<android.support.v7.widget.CardView
cardBackgroundColor="@android:color/white"
cardCornerRadius="4dp"
cardElevation="6dp"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_margin="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:padding="5dp">
<EditText
android:id="@+id/et_info_type"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#00000000"
android:ellipsize="none"
android:gravity="center_vertical"
android:hint="@string/info_type"
android:inputType="text"
android:padding="5dp"
android:textColor="@android:color/black"
android:textColorHint="#C7C7C7" />
</LinearLayout>
</android.support.v7.widget.CardView>
<!--以上部分为需要动态创建的部分-->
</LinearLayout>
</ScrollView> @Override
public void onClick(View view) {
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.ll_info_add);
CardView newSingleCV = generateSingleLayout();
linearLayout.addView(newSingleCV);
}
/**
* 动态生成控件部分
* @return
*/
public CardView generateSingleLayout() {
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
linearLayout.setPadding(dip2px(context, 5), dip2px(context, 5), dip2px(context, 5), dip2px(context, 5));
LinearLayout.LayoutParams LL_LP = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
EditText editText = new EditText(this);
editText.setWidth(0);
editText.setHeight(RelativeLayout.LayoutParams.MATCH_PARENT);
editText.setInputType(InputType.TYPE_CLASS_TEXT);
editText.setGravity(Gravity.CENTER_VERTICAL);
editText.setPadding(dip2px(context, 5), dip2px(context, 5), dip2px(context, 5), dip2px(context, 5));
editText.setBackgroundColor(0);
editText.setTextColor(Color.BLACK);
editText.setHintTextColor(Color.rgb(199, 199, 199));
linearLayout.addView(editText, LL_LP);
CardView cardView = new CardView(this);
cardView.setLayoutParams(new CardView.LayoutParams(
CardView.LayoutParams.MATCH_PARENT, // width
dip2px(context, 50))); // height
cardView.setPreventCornerOverlap(true);
cardView.setCardBackgroundColor(Color.WHITE);
cardView.setRadius(dip2px(context, 4));
cardView.setCardElevation(dip2px(context, 6));
// cardView.setBackgroundColor(Color.GREEN);
final CardView.LayoutParams CV_LP = new CardView.LayoutParams(CardView.LayoutParams.MATCH_PARENT, dip2px(context, 50));
CV_LP.setMargins(dip2px(context, 5), dip2px(context, 5), dip2px(context, 5), dip2px(context, 5));
cardView.addView(linearLayout, CV_LP);
return cardView;
}
//dip转像素
public static int dip2px(Context context, float dipValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dipValue * scale + 0.5f);
}
}