【发布时间】:2014-11-17 14:36:49
【问题描述】:
首先,我可以提供的代码有限,因为这是用于工作的。有了那个...
我正在以编程方式添加视图。高度是完美的。父视图上没有填充,添加的视图上也没有边距,但左侧有一个 8dp 到 12dp 的填充/边距/间隙,我无法弄清楚。我真的需要这个视图来扩展父级的整个宽度。非常感谢任何帮助。
父布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/baseLayoutRootParent"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
添加视图的布局
<?xml version="1.0" encoding="utf-8"?>
<ImageView
android:id="@+id/viewImage"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_centerVertical="true"
android:padding="2dp" />
<LinearLayout
android:id="@+id/textHolder"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:layout_toEndOf="@+id/viewImage"
android:layout_toRightOf="@+id/viewImage"
android:gravity="center_vertical"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_padding"
android:paddingEnd="@dimen/activity_horizontal_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingStart="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_padding">
<TextView
android:id="@+id/viewText"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
app:fontName="fonts/lato_regular.ttf" />
</LinearLayout>
<CheckBox
android:id="@+id/viewCheckBox"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:paddingBottom="@dimen/activity_vertical_padding"
android:paddingEnd="@dimen/activity_horizontal_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingStart="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_padding"
android:visibility="gone" />
<LinearLayout
android:id="@+id/lineDivider"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_alignParentBottom="true"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:background="@android:color/darker_gray"
android:orientation="horizontal" />
我将视图添加到父级的代码:
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.child_view, rootParent, false);
view.setBackgroundColor(getResources().getColor(R.color.blue));
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, viewHeight);
lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)
lp.addRule(RelativeLayout.ALIGN_PARENT_START, RelativeLayout.TRUE);
view.setLayoutParams(lp);
view.setId(R.id.viewID);
ImageView iv = (ImageView) view.findViewById(R.id.viewImage);
Picasso.with(Activity.this).load(file.image).placeholder(R.drawable.ic_launcher).transform(new CircleTransform()).into(iv);
TextView tv = (TextView) view.findViewById(R.id.viewText);
tv.setText(file.name());
tv.setTextColor(getResources().getColor(android.R.color.white));
(rootParent).addView(view);
// these are set from another activity. It's the x/y coordinates of a view that this view copies
view.setX(viewLocation[0]);
view.setY(viewLocation[1] + (findViewById(R.id.toolbar)).getHeight());
结果如下:
【问题讨论】:
-
你能告诉我们它是 xml 中的哪个元素吗?
-
我包含了根视图的 XML 和要添加的视图的 XML。我希望子视图填充根的整个宽度,但它不是
-
这篇文章值得-1怎么办?
-
我正在添加整个子视图
标签: android android-layout android-relativelayout