2014-08-29 53 views
0

我正在使用Google Maps API v2,並由於擁有自定義的InfoWindow而忽略了getInfoContents()InfoWindowAdapter。在getInfoContents()裏面,我給一個xml充氣,其中包含一個ImageView和三個TextViews防止出現在屏幕外的視圖

當我在snippet中設置的文本大於視圖時,我的問題就出現了,那麼計數電視就被設置在視圖之外。上面有一些截圖。

enter image description hereenter image description here enter image description here

這裏我的XML佈局:

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

    <ImageView 
     android:id="@+id/categoryIcon" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:layout_alignBottom="@+id/count" 
     android:src="@drawable/ic_launcher"/> 

    <LinearLayout 
     android:id="@+id/text" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     android:layout_toRightOf="@+id/categoryIcon" 
     android:layout_centerVertical="true" 
     android:paddingLeft="10dp" 
     android:paddingRight="10dp"> 

     <TextView 
      android:id="@+id/title" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Title" 
      android:textStyle="bold" /> 

     <TextView 
      android:id="@+id/snippet" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Snippet" /> 
    </LinearLayout> 

    <TextView 
     android:id="@+id/count" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="#111177" 
     android:text="0" 
     android:textSize="20sp" 
     android:padding="10dp" 
     android:textColor="#FFF" 
     android:layout_toRightOf="@+id/text" 
     android:singleLine="true" /> 

</RelativeLayout> 

我怎樣才能改變這種佈局讓屏幕裏的計數TextView的,並在多片斷?

我想始終顯示計數TextView。

更新: 最後,與西蒙侯爵的答案我意識到一種方法來實現這一點。在這裏,新的代碼:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" > 

    <ImageView 
     android:id="@+id/categoryIcon" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:layout_alignBottom="@+id/text" 
     android:src="@drawable/ic_launcher"/> 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/text" 
     android:orientation="horizontal" 
     android:gravity="center_vertical" 
     android:layout_toRightOf="@+id/categoryIcon"> 

     <LinearLayout 
     android:layout_width="0dp" 
     android:layout_weight="1" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     android:paddingLeft="10dp" 
     android:paddingRight="10dp"> 

     <TextView 
      android:id="@+id/title" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Title" 
      android:textStyle="bold" /> 

     <TextView 
      android:id="@+id/snippet" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Snippeta" /> 
    </LinearLayout> 

    <TextView 
     android:id="@+id/count" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="#111177" 
     android:text="0" 
     android:textSize="20sp" 
     android:padding="10dp" 
     android:textColor="#FFF" 
     android:singleLine="true" /> 

    </LinearLayout> 

</RelativeLayout> 

回答

2

您應該將其添加到的LinearLayout:

android:layout_width="0dp" 
android:layout_weight="1" 

新的佈局文件:

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

    <ImageView 
     android:id="@+id/categoryIcon" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:layout_alignBottom="@+id/count" 
     android:src="@drawable/ic_launcher"/> 

    <LinearLayout 
     android:id="@+id/text" 
     android:layout_width="0dp" 
     android:layout_weight="1" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     android:layout_toRightOf="@+id/categoryIcon" 
     android:layout_centerVertical="true" 
     android:paddingLeft="10dp" 
     android:paddingRight="10dp"> 

     <TextView 
      android:id="@+id/title" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Title" 
      android:textStyle="bold" /> 

     <TextView 
      android:id="@+id/snippet" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Snippet" /> 
    </LinearLayout> 

    <TextView 
     android:id="@+id/count" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="#111177" 
     android:text="0" 
     android:textSize="20sp" 
     android:padding="10dp" 
     android:textColor="#FFF" 
     android:layout_toRightOf="@+id/text" 
     android:singleLine="true" /> 

</RelativeLayout> 
+0

在RelativeLayout中使用weight並不影響視圖,s o我所做的是創建另一個包含另一個Linear和count的LinearLayout。我用解決方案更新我的答案。 – 2014-08-29 17:01:29

+0

爲我工作!謝謝。 @Simon拜託,你認爲你可以幫我解決這個問題嗎? http://stackoverflow.com/questions/25598696/recommended-way-order-to-read-data-from-a-webservice-parse-that-data-and-inse – Axel 2014-09-01 23:10:07

1

一個解決方案,伯爵的TextView總是出現,如果你想在右邊是以下內容:

  • 把co在的LinearLayout前UNT文本視圖,並添加 機器人:layout_alignParentRight = 「真」
  • 並添加機器人:在線性佈局

    <TextView 
        android:id="@+id/count" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_alignParentRight="true" 
        android:layout_alignParentTop="true" 
        android:background="#00F" 
        android:text="Large Text" 
        android:textAppearance="?android:attr/textAppearanceLarge" /> 
    
    <LinearLayout 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_alignParentLeft="true" 
        android:layout_alignParentTop="true" 
        android:layout_toLeftOf="@id/count" 
        android:orientation="vertical" > 
    
        <TextView 
         android:id="@+id/title" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:textAppearance="?android:attr/textAppearanceLarge" /> 
    
        <TextView 
         android:id="@+id/snippet" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:textAppearance="?android:attr/textAppearanceLarge" /> 
    
    </LinearLayout> 
    
    layout_toLeftOf = 「@ ID /計數」

+0

這將始終工作,文字大於視圖,但是當視圖更小時,視圖將具有最大尺寸,因爲視圖是alignedParentRight,android使用左上角來設置視圖的邊界。所以它會有寬度= MATCH_PARENT的外觀 – 2014-08-29 16:53:28