添加视图不填充宽度

时间:2014-11-17 14:36:50

标签: android android-layout android-relativelayout

首先,我限制了我可以提供的代码,因为这是为了工作。有了......

我以编程方式添加视图。高度很完美。父视图上没有填充,添加的视图没有边距,但左侧有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());

以下是结果:

enter image description here

1 个答案:

答案 0 :(得分:0)

首先删除添加的布局中的每个元素上的marginLeft / marginStart,然后查看它是哪一个。

这个问题有点不清楚,因为很难在你的图片和给定的布局之间建立联系。哪个是viewCheckBox,哪里是viewImage等。