在RelativeLayout中对齐垂直视图的左边缘

时间:2014-05-11 15:03:47

标签: android android-layout

enter image description here

如何使500 (TextView)匹配上方checkbox的左边缘?

我正在使用RelativeLayout

<ImageView
    android:id="@+id/group_indicator"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:src="@drawable/expander_open_holo_dark" />

<TextView
    android:id="@+id/lblListHeader"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
    android:textSize="17sp" />

<CheckBox
    android:id="@+id/GroupCheckBox"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_alignParentRight="true"
    android:focusable="false"
    android:paddingRight="10dp"
    android:scaleX="2"
    android:scaleY="2" />

<SeekBar
    android:id="@+id/GroupSeekbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@id/lblListHeader"
    android:layout_alignParentLeft="true"
    android:layout_toLeftOf="@+id/GroupSeekCount"
    android:layout_below="@id/lblListHeader"
    android:focusable="false"
    android:max="1000"
    android:paddingBottom="0dp"
    android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
    android:paddingTop="20dp" >
</SeekBar>

<TextView
    android:id="@+id/GroupSeekCount"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:paddingRight="10dp"
    android:paddingTop="20dp" 
    android:text="500"
    android:layout_below="@id/GroupCheckBox"

    />

3 个答案:

答案 0 :(得分:1)

我会以不同的方式解决这个问题。

尝试将扩展器和列表标题分组为水平线性布局,然后将Checkbox和500文本分组为垂直线性布局。最后,这两个linearlayout应该嵌套在水平线性布局中。

答案 1 :(得分:1)

首先定义CheckBox,然后定义TextView 然后将TextView的layout_width设置为match_parent(以便占用剩余空间)

此外,ImageView可以成为TextView(复合drawable)的一部分,以减少控件数量:
为此:在TextView定义中,设置以下属性:

android:drawableLeft="@drawable/expander_open_holo_dark"
android:drawablePadding="8dp"

然后您可以通过代码修改组指示符:

Drawable drw = getContext().getResources().getDrawable(R.drawable.expander_open_holo_dark);
lblListHeader.setCompoundDrawablesWithIntrinsicBounds(drw, null, null, null);

更好(始终牢记优化设计的目的),您可以使用CheckedTextView控件,以便将图像,文本和复选标记放在一个控件中(因为它扩展了TextView,它可以容纳复合drawables以及TextView确实如此。

当然,您可以将刻度标记向右移动。

答案 2 :(得分:1)

// try this way,hope this will help you...

<RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="5dp">

    <ImageView
        android:id="@+id/group_indicator"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:src="@drawable/expander_open_holo_dark" />

    <CheckBox
        android:id="@+id/GroupCheckBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:focusable="false"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:scaleX="2"
        android:scaleY="2" />

    <TextView
        android:id="@+id/lblListHeader"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@id/group_indicator"
        android:layout_toLeftOf="@id/GroupCheckBox"
        android:textSize="17sp" />

    <SeekBar
        android:id="@+id/GroupSeekbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:focusable="false"
        android:max="1000"
        android:paddingBottom="0dp"
        android:layout_below="@id/lblListHeader"
        android:layout_toLeftOf="@id/GroupCheckBox"
        android:layout_alignBaseline="@id/lblListHeader"
        android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
        android:paddingTop="20dp" />

    <TextView
        android:id="@+id/GroupSeekCount"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_below="@id/GroupCheckBox"
        android:layout_toRightOf="@id/GroupSeekbar"
        android:paddingTop="10dp"
        android:text="500"/>

</RelativeLayout>