如何在相对布局中将此文本设置在图像的左侧

时间:2013-06-09 17:49:59

标签: android relativelayout

Snap

            <RelativeLayout
            android:id="@+id/row1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:background="@drawable/main_row" >

            <TextView
                android:id="@+id/alarm_clock"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:gravity="center_horizontal"
                android:text="7:15 AM"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:textSize="35dp" />

            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignLeft="@+id/alarm_clock"
                android:layout_below="@+id/alarm_clock"
                android:layout_marginLeft="14dp"
                android:text="Next Alarm"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textSize="29dp" />

            <TextView
                android:id="@+id/next_alarm_date"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignLeft="@+id/alarm_clock"
                android:layout_below="@+id/textView2"
                android:layout_marginLeft="14dp"
                android:text="Thursday 27th July"
                android:textColor="#ffffff"
                android:textSize="19dp" />

            <TextView
                android:id="@+id/next_alarm_time"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignLeft="@+id/alarm_clock"
                android:layout_below="@+id/next_alarm_date"
                android:layout_marginLeft="14dp"
                android:text="8:05 AM"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:textSize="27sp" />

            <TextView
                android:id="@+id/next_alarm_time_left"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignLeft="@+id/alarm_clock"
                android:layout_below="@+id/next_alarm_time"
                android:layout_marginLeft="14dp"
                android:text="In 5 hrs 15 mins"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textSize="20dp" />

            <TextView
                android:id="@+id/add_alarm_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignBottom="@+id/next_alarm_time_left"
                android:layout_alignLeft="@+id/add_alarm_icon"
                android:layout_marginBottom="2dp"
                android:text="Add Alarm"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:textSize="13dp" />

            <ImageView
                android:id="@+id/add_alarm_icon"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_above="@+id/add_alarm_text"
                android:layout_alignRight="@+id/alarm_clock"
                android:layout_marginRight="17dp"
                android:src="@drawable/add_alarm" />
        </RelativeLayout>

由于屏幕尺寸的原因,在日期和日期下面8:05 AM的时间超出了某些手机的时钟图标。每当文本触及时钟图标时,我怎么能这样做呢?剩下的文字(时间)进入一个新的行。

谢谢:)

2 个答案:

答案 0 :(得分:0)

我是靠自己做的:p

<RelativeLayout
            android:id="@+id/row1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:background="@drawable/main_row" >

            <TextView
                android:id="@+id/alarm_clock"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:gravity="center_horizontal"
                android:text="7:15 AM"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:textSize="35dp" />

            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignLeft="@id/alarm_clock"
                android:layout_below="@id/alarm_clock"
                android:layout_marginLeft="14dp"
                android:text="Next Alarm"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textSize="29dp" />

            <TextView
                android:id="@+id/next_alarm_date"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignLeft="@id/alarm_clock"
                android:layout_below="@id/textView2"
                android:layout_marginLeft="14dp"
                android:text="Thursday 27th July"
                android:textColor="#ffffff"
                android:textSize="19dp" />

            <TextView
                android:id="@+id/next_alarm_time"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignLeft="@id/alarm_clock"
                android:layout_below="@id/next_alarm_date"
                android:layout_marginLeft="14dp"
                android:layout_toLeftOf="@+id/add_alarm_icon"
                android:text="8:05 AM"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:textSize="27sp" />

            <TextView
                android:id="@+id/next_alarm_time_left"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignLeft="@id/alarm_clock"
                android:layout_below="@id/next_alarm_time"
                android:layout_marginLeft="14dp"
                android:text="In 5 hrs 15 mins"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textSize="20dp" />
            <ImageView
                android:id="@+id/add_alarm_icon"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_above="@+id/add_alarm_text"
                android:layout_alignRight="@id/alarm_clock"
                android:layout_marginRight="17dp"
                android:src="@drawable/add_alarm" />

            <TextView
                android:id="@+id/add_alarm_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignBottom="@id/next_alarm_time_left"
                android:layout_alignLeft="@id/add_alarm_icon"
                android:layout_marginBottom="2dp"
                android:text="Add Alarm"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:textSize="13dp" />


        </RelativeLayout>

答案 1 :(得分:0)

你应该对你的布局做一些改进,比如创建一个color.xml来设置颜色,string.xml来设置应用程序上使用的文本,而不是使用dp作为textSize,使用sp。

在此处查看一些构建用户界面的良好做法:http://developer.android.com/guide/topics/ui/index.html

Color.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
   <color name="white">#ffffff</color>
</resources>

String.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

   <string name="next_alarm_time_left">In 5 hrs 15 mins</string>
   <string name="add_alarm_text">Add Alarm</string>
   <string name="next_alarm_time">8:05 AM</string>
   <string name="next_alarm_date">Thursday 27th July</string>
   <string name="textView2">Next Alarm</string>
   <string name="alarm_clock">7:15 AM</string>

</resources>

布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/row1"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_marginTop="10dp">

  <TextView
   android:id="@+id/alarm_clock"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:layout_alignParentTop="true"
   android:gravity="center_horizontal"
   android:text="@string/alarm_clock"
   android:textAppearance="?android:attr/textAppearanceLarge"
   android:textSize="35sp" />

  <TextView
   android:id="@+id/textView2"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_alignLeft="@+id/alarm_clock"
   android:layout_below="@+id/alarm_clock"
   android:layout_marginLeft="14dp"
   android:text="@string/textView2"
   android:textAppearance="?android:attr/textAppearanceMedium"
   android:textSize="29sp" />

  <TextView
   android:id="@+id/next_alarm_date"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_alignLeft="@+id/alarm_clock"
   android:layout_below="@+id/textView2"
   android:layout_marginLeft="14dp"
   android:text="@string/next_alarm_date"
   android:textColor="@color/white"
   android:textSize="19sp" />

  <TextView
   android:id="@+id/next_alarm_time"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_alignLeft="@+id/alarm_clock"
   android:layout_below="@+id/next_alarm_date"
   android:layout_marginLeft="14dp"
   android:text="@string/next_alarm_time"
   android:textAppearance="?android:attr/textAppearanceLarge"
   android:textSize="27sp" />

  <TextView
   android:id="@+id/next_alarm_time_left"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_alignLeft="@+id/alarm_clock"
   android:layout_below="@+id/next_alarm_time"
   android:layout_marginLeft="14dp"
   android:text="@string/next_alarm_time_left"
   android:textAppearance="?android:attr/textAppearanceMedium"
   android:textSize="20sp" />

  <TextView
   android:id="@+id/add_alarm_text"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_alignBottom="@+id/next_alarm_time_left"
   android:layout_alignLeft="@+id/add_alarm_icon"
   android:layout_marginBottom="2dp"
   android:text="@string/add_alarm_text"
   android:textAppearance="?android:attr/textAppearanceLarge"
   android:textSize="13sp" />

  <ImageView
   android:id="@+id/add_alarm_icon"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_above="@+id/add_alarm_text"
   android:layout_alignRight="@+id/alarm_clock"
   android:layout_marginRight="17dp" />

</RelativeLayout>