【求助】动态添加cardview设置margin无效,求解

kaker 2017-01-10 03:42:38

上面这个是我要达到的效果




上面这两张是动态创建的实际效果,上下左右边距没有达到预期效果

下面是XML的layout布局文件代码
<?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);
}
}



请各位大神前辈们指点下,怎么才能达到第一张图片的那种效果啊?新手学习,望不吝赐教!万分感谢!
...全文
743 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
kaker 2017-01-10
  • 打赏
  • 举报
回复
谢谢群里的大神 幻城二 的帮助,原因是对LayoutParams的使用不了解, 请大家参看这里的说明 http://blog.csdn.net/arest/article/details/6362819
kaker 2017-01-10
  • 打赏
  • 举报
回复
外面这个LinearLayout没有重写吧
怀君 2017-01-10
  • 打赏
  • 举报
回复
引用 3 楼 kaker 的回复:
外面这个LinearLayout是包含所有cardview的容器,设置它的padding只能对左右编辑有效果,每隔cardview的上下边距有起不了作用了
那是因为你在Activity里面,用JAVA代码重写了布局管理器。
kaker 2017-01-10
  • 打赏
  • 举报
回复
外面这个LinearLayout是包含所有cardview的容器,设置它的padding只能对左右编辑有效果,每隔cardview的上下边距有起不了作用了
怀君 2017-01-10
  • 打赏
  • 举报
回复
你给线性布局 padding不也可以。
kaker 2017-01-10
  • 打赏
  • 举报
回复
CSDN的这个编辑器图片怎么不能编辑大小了啊

80,492

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧