2014-09-04 17 views
1

目前我的XML看起來像下面對準另一個的TextView的TextView如下: enter image description here如何中心在ListView

我試圖做的是有「日」(而它與實際日期代替(如03 ,24))水平集中在月份之下('月份'也被替換爲MMM格式的月份字符串),以便水平日中心與月份的水平中心匹配。旁邊還有一些其他的TextView對象,比如事件名稱和事件時間。

我基於我的XML部分關閉了How to center align a textview below imageview as a title in a List view,但日期稍微偏離了右側,我不太清楚如何糾正問題,儘管從RelativeLayout文檔中查找屬性。

http://developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.html

events_info.xml(佈局的ListView對象):

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/eventsRelLayout" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    android:padding="6dip"> 


    <TextView 
     android:id="@+id/month" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Month" /> 


    <TextView 
     android:id="@+id/day" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/month" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true" 
     android:singleLine="true" 
     android:text="Day" /> 


    <TextView 
     android:id="@+id/name" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_toRightOf="@id/month" 
     android:paddingLeft="10dp" 
     android:paddingRight="0dp" 
     android:text="Name" /> 

    <TextView 
     android:id="@+id/time" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/name" 
     android:layout_toRightOf="@id/month" 
     android:paddingLeft="10dp" 
     android:paddingRight="0dp" 
     android:text="Time" /> 

fragment_event_list.xml(ListView中):

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="fish.glow.EventFragment"> 

    <ListView 
     android:id="@+id/list" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

</FrameLayout> 

回答

3

嘗試這種情況:

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

<RelativeLayout 
    android:id="@+id/rl_left" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 

    <TextView 
     android:id="@+id/month" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="month month" /> 

    <TextView 
     android:id="@+id/day" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/month" 
     android:layout_centerHorizontal="true" 
     android:gravity="center" 
     android:text="day" /> 

</RelativeLayout> 


<TextView 
    android:id="@+id/name" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="10dp" 
    android:layout_toRightOf="@+id/rl_left" 
    android:text="name" /> 


<TextView 
    android:id="@+id/time" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/name" 
    android:layout_marginLeft="10dp" 
    android:layout_toRightOf="@+id/rl_left" 
    android:text="time" /> 

</RelativeLayout> 
+0

這將月份放置在左上角,右上角的日期(應該在左下角),左下角的名稱(應該在右上角)以及右下角的時間。 – 2014-09-04 22:51:00

+0

那麼,你必須改變文字... – 2014-09-04 22:57:47

+0

這使得日和月水平對齊到左側。我試圖讓一天在月中心水平對齊。對不起,有任何困惑。 :X – 2014-09-04 23:13:22

1
<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/eventsRelLayout" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    android:padding="6dip"> 

<LinearLayout 
    android:id="@+id/date" 
    android:orienation="vertical" 
    android:gravity="center" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 

    <TextView 
     android:id="@+id/month" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Month" /> 


    <TextView 
     android:id="@+id/day" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/month" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true" 
     android:singleLine="true" 
     android:text="Day" /> 
</LinearLayout> 


    <TextView 
     android:id="@+id/name" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_toRightOf="@id/date" 
     android:paddingLeft="10dp" 
     android:paddingRight="0dp" 
     android:text="Name" /> 

    <TextView 
     android:id="@+id/time" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/name" 
     android:layout_toRightOf="@id/date" 
     android:paddingLeft="10dp" 
     android:paddingRight="0dp" 
     android:text="Time" />