2011-12-16 51 views
1

上下文:我有一个基本上由一个RelativeLayout包装一堆TextViews组成的小部件。这就是我想要的小工具,在视觉上看起来,其次是在XML布局代码:RelativeLayout:layout_marginLeft不一致的行为

Expected layout

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/alarm_widget_layout" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    > 
    <TextView 
    android:id="@+id/alarm_time" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:text="8:30" 
    android:textSize="40sp" 
    /> 
    <TextView 
    android:id="@+id/alarm_am_pm" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_toRightOf="@id/alarm_time" 
    android:layout_marginLeft="2dp" 
    android:layout_alignTop="@id/alarm_time" 
    android:textSize="18sp" 
    android:text="AM" 
    /> 
    <TextView 
    android:id="@+id/alarm_days" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:layout_below="@id/alarm_time" 
    android:textSize="16sp" 
    android:text="M T W T F S S" 
    /> 
    <TextView 
    android:id="@+id/toggle_indicator" 
    android:layout_height="8sp" 
    android:layout_width="80sp" 
    android:layout_below="@id/alarm_days" 
    android:layout_centerHorizontal="true" 
    android:background="@drawable/toggle_button_oval" 
    /> 
</RelativeLayout> 

问:我难倒与layout_marginLeft在下列情况下不一致的行为:

  1. 当这些小部件一个的LinearLayout中,AM/PM文本在右上方内垂直堆叠,需要机器人:layout_marginLeft =“15dp”看起来像它在画面进行如上所述。
  2. 但是,当部件堆叠在2x2 TableLayout中时,AM/PM文本需要android:layout_marginLeft =“2dp”才能正确显示。

为什么我看到这种不一致的行为? layout_marginLeft使用什么作为“原点”?

回答

1

由于您使用的是垂直LinearLayout(orientation = vertical),因此android:layout_marginLeft会将屏幕的最左端作为原点,因此需要较大的倾角值〜15。但是,如果您一直使用orientation = horizo​​ntal,则原点将是am_pm之前的元素的结尾,因此您需要较小的dip值〜2。 类似于相对表格布局的情况。由于在使用RelativeLayout时,您提到了android:layout_toRightOf =“@ id/alarm_time”,原点将是alarm_time的结尾,因此需要较小的倾角值。

+0

感谢您的回复@SaurabhVerma。我不确定我是否正确理解这一点。你是说layout_marginLeft使用的原点是基于RelativeLayout的堆叠方式吗?如果RelativeLayout堆叠在垂直LinearLayout中,则原点位于屏幕的最左侧。但是,如果将其堆叠在2x2 TableLayout中,则原点是@ id/alarm_time(由于layout_toRightOf属性)。这非常令人困惑!这种行为是否在任何地方记录? – 2011-12-16 10:31:24