在布局中正确放置textview

时间:2013-07-18 09:08:48

标签: android android-layout view

我有一个自定义对话框,它是一系列按钮,选择器和viewwitchers。我正在尝试将textview显示在setDate和setTime按钮上方。目前它在按钮顶部显示文本视图。

如何在时间和日期按钮上方显示textview?感谢。

enter image description here

>

 <?xml version="1.0" encoding="UTF-8"?> 

<RelativeLayout 
>     
>     android:padding="5dip" 
>     android:fitsSystemWindows="true" 
>     android:layout_height="wrap_content" 
>     android:layout_width="fill_parent" 
>     android:id="@+id/DateTimePicker" 
>     xmlns:android="http://schemas.android.com/apk/res/android">
>     
>     
>     
>     <LinearLayout 
>         
>         android:layout_height="wrap_content" 
>         android:layout_width="fill_parent" 
>         android:id="@+id/linearlayoutdatepickermessage" 
>         android:layout_marginTop="5dip"> 
>     
>     
>     <TextView
>         android:id="@+id/textviewdatetimepickermessage"
>         android:layout_width="wrap_content"
>         android:layout_height="wrap_content"
>         android:layout_gravity="center"
>         
>          />
>     
>     </LinearLayout>
>     
>     <LinearLayout 
>         
>         android:layout_height="wrap_content" 
>         android:layout_width="fill_parent" 
>         android:id="@+id/ViewSwitchButtons" 
>         android:layout_marginBottom="5dip"> 
>         
>         
>         
>         <Button 
>             android:layout_height="wrap_content" 
>             android:layout_width="0dip" 
>             android:id="@+id/SwitchToDate" 
>             android:text="Set date" 
>             android:enabled="false" 
>             android:layout_weight="1"/>
>         
>         <Button 
>             android:layout_height="wrap_content" 
>             android:layout_width="0dip" 
>             android:id="@+id/SwitchToTime" 
>             android:text="Set time" 
>             android:layout_weight="1"/>
>         
>         </LinearLayout> 
>         
>         <ViewSwitcher 
>             android:layout_height="wrap_content" 
>             android:layout_width="fill_parent" 
>             android:id="@+id/DateTimePickerVS" 
>             android:layout_below="@+id/ViewSwitchButtons" 
>             android:outAnimation="@android:anim/fade_out" 
>             android:inAnimation="@android:anim/fade_in"> 
>             
>             
>                 
>                 <LinearLayout 
>                     android:layout_height="fill_parent" 
>                     android:layout_width="fill_parent" 
>                     android:id="@+id/TimeLayout" 
>                     android:fillViewport="true"> 
>                     
>                     <TimePicker 
>                         android:layout_height="wrap_content" 
>                         android:layout_width="fill_parent" 
>                         android:id="@+id/TimePicker" 
>                         android:layout_marginRight="5dip" 
>                         android:layout_marginLeft="5dip"/>
>                     
>                     </LinearLayout> 
>                     
>                 <LinearLayout android:layout_height="fill_parent" 
>                 android:layout_width="fill_parent" 
>                 android:id="@+id/DateLayout" 
>                 android:fillViewport="true"> 
>                 
>                 <DatePicker 
>                     android:layout_height="wrap_content" 
>                     android:layout_width="fill_parent" 
>                     android:id="@+id/DatePicker" 
>                     android:layout_marginRight="5dip" 
>                     android:layout_marginLeft="5dip"/>
>                 
>                 </LinearLayout> 
>                     
>                     </ViewSwitcher> 
>                     
>                     <LinearLayout 
>                         android:layout_height="wrap_content" 
>                         android:layout_width="fill_parent" 
>                         android:id="@+id/ControlButtons" 
>                         android:layout_below="@+id/DateTimePicker" 
>                         android:paddingTop="185dip"> 
>                         
>                         <Button 
>                             android:layout_height="wrap_content" 
>                             android:layout_width="0dip" 
>                             android:id="@+id/SetDateTime" 
>                             android:text="@android:string/ok" 
>                             android:layout_weight="1"/> 
>                             
>                             <Button 
>                                 android:layout_height="wrap_content" 
>                                 android:layout_width="0dip" 
>                                 android:id="@+id/ResetDateTime" 
>                                 android:text="Reset" 
>                                 android:layout_weight="1"/>
>                             
>                         <!--     <Button 
>                                 android:layout_height="wrap_content" 
>                                 android:layout_width="0dip" 
>                                 android:id="@+id/CancelDialog" 
>                                 android:text="@android:string/cancel" 
>                                 android:layout_weight="1"/>  -->
>                                 
>                                 </LinearLayout> 
>                                 
>                                 </RelativeLayout>

[EDIT1] enter image description here

[EDIT2] enter image description here

[EDIT3]

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout 

        android:layout_height="wrap_content" 
        android:layout_width="fill_parent" 
        android:orientation="vertical"
        xmlns:android="http://schemas.android.com/apk/res/android"> 



    <LinearLayout 

        android:layout_height="wrap_content" 
        android:layout_width="fill_parent" 
        android:id="@+id/linearlayoutdatepickermessage" 
        android:layout_marginTop="5dip"> 


    <TextView
        android:id="@+id/textviewdatetimepickermessage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"

         />

    </LinearLayout>

    <LinearLayout 

        android:layout_height="wrap_content" 
        android:layout_width="fill_parent" 
        android:id="@+id/ViewSwitchButtons" 
        android:layout_marginBottom="5dip"
       > 



        <Button 
            android:layout_height="wrap_content" 
            android:layout_width="0dip" 
            android:id="@+id/SwitchToDate" 
            android:text="Set date" 

            android:layout_weight="1"/>

        <Button 
            android:layout_height="wrap_content" 
            android:layout_width="0dip" 
            android:id="@+id/SwitchToTime"
            android:enabled="false" 
            android:text="Set time" 
            android:layout_weight="1"/>

        </LinearLayout> 

        <ViewSwitcher 
            android:layout_height="wrap_content" 
            android:layout_width="fill_parent" 
            android:id="@+id/DateTimePickerVS" 

            android:outAnimation="@android:anim/fade_out" 
            android:inAnimation="@android:anim/fade_in"> 



                <LinearLayout 
                    android:layout_height="fill_parent" 
                    android:layout_width="fill_parent" 
                    android:id="@+id/TimeLayout" 
                    android:fillViewport="true"> 

                    <TimePicker 
                        android:layout_height="wrap_content" 
                        android:layout_width="fill_parent" 
                        android:id="@+id/TimePicker" 
                        android:layout_marginRight="5dip" 
                        android:layout_marginLeft="5dip"/>

                    </LinearLayout> 

                <LinearLayout android:layout_height="fill_parent" 
                android:layout_width="fill_parent" 
                android:id="@+id/DateLayout" 
                android:fillViewport="true"> 

                <DatePicker 
                    android:layout_height="wrap_content" 
                    android:layout_width="fill_parent" 
                    android:id="@+id/DatePicker" 
                    android:layout_marginRight="5dip" 
                    android:layout_marginLeft="5dip"/>

                </LinearLayout> 

                    </ViewSwitcher> 

                    <LinearLayout 
                        android:layout_height="wrap_content" 
                        android:layout_width="fill_parent" 
                        android:id="@+id/ControlButtons" 

                        android:paddingTop="185dip"> 

                        <Button 
                            android:layout_height="wrap_content" 
                            android:layout_width="0dip" 
                            android:id="@+id/SetDateTime" 
                            android:text="@android:string/ok" 
                            android:layout_weight="1"/> 

                            <Button 
                                android:layout_height="wrap_content" 
                                android:layout_width="0dip" 
                                android:id="@+id/ResetDateTime" 
                                android:text="Reset" 
                                android:layout_weight="1"/>

                        <!--     <Button 
                                android:layout_height="wrap_content" 
                                android:layout_width="0dip" 
                                android:id="@+id/CancelDialog" 
                                android:text="@android:string/cancel" 
                                android:layout_weight="1"/>  -->

                                </LinearLayout> 

                                </LinearLayout>

2 个答案:

答案 0 :(得分:1)

ViewSwitchButtons LinearLayout上,输入:

android:layout_below="@+id/linearlayoutdatepickermessage"

好的,您的布局包括:

  1. LinearLayout1
  2. ViewSwitcher
  3. LinearLayout2
  4. LinearLayout3
  5. 如果你想使用RelativeLayout,你应该添加:

    1. android:layout_below="ID_LinearLayout1"到ViewSwitcher
    2. android:layout_below="ID_ViewSwitcher"到LinearLayout2
    3. android:layout_below="ID_LinearLayout2"到LinearLayout3
    4. 另一种选择是将您的RelativeLayout家长更改为LinearLayout <{1}}

答案 1 :(得分:0)

{p}在RelativeLayout中,定位视图的默认参数为alignParentTop = "true",因此在您的代码中,您没有指定放置Layout Buttons的位置(设置日期/设置时间),因此它将放在顶部(布局为TextView msg)。所以要将你的按钮放在textView下面修改你的xml布局,如下所示:

<LinearLayout         
    android:layout_height="wrap_content" 
    android:layout_width="fill_parent" 
    android:id="@+id/ViewSwitchButtons" 
    android:layout_marginBottom="5dip"
    android:layout_marginTop="5dip"
    android:layout_marginBottom="5dip"
    android:layout_below="@+id/linearlayoutdatepickermessage" <!--this will put your buttons below the textView-->
>