2012-10-22 60 views
0

我需要幫助才能讓我的圖像和文本的佈局工作。Android,佈局圖像和文本

我希望圖像縮放到與我的TextView使用的高度相同。 我還希望圖像留給文本。 例如#在我的圖像

# "My Text" 

## "My long text 
## is this here" 

圖像的witdh將會使圖像保持方面。

我的代碼是現在這個,基本的佈局工作,但形象是大。

 <LinearLayout 
      android:id="@+id/packet_list_view_layout_subvehicle" android:layout_width="fill_parent" 
      android:layout_weight="1" android:orientation="horizontal" 
      android:gravity="left" android:layout_height="wrap_content"> 
      <ImageView 
       android:id="@+id/packet_list_view_layout_subvehicle_image" 
       android:src="@drawable/packet" 
       android:scaleType="fitCenter" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" /> 
      <TextView 
       android:id="@+id/packet_list_view_layout_subvehicle_text" 
       android:textAppearance="?android:attr/textAppearanceSmall" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:text="===================" 
       /> 

編輯1

A以外的嘗試給我相同的結果。

 <LinearLayout 
      android:id="@+id/packet_list_view_layout_subvehicle" 
      android:layout_width="fill_parent" 
      android:layout_weight="1" 
      android:orientation="horizontal" 
      android:gravity="left" 
      android:layout_height="wrap_content" 
     > 
      <ImageView 
       android:id="@+id/packet_list_view_layout_subvehicle_image" 
       android:layout_width="wrap_content" 
       android:layout_height="fill_parent" 
       android:scaleType="fitCenter" 
       android:src="@drawable/packet" /> 
      <TextView 
       android:id="@+id/packet_list_view_layout_subvehicle_text" 
       android:layout_width="wrap_content" 
       android:layout_height="fill_parent" 
       android:text="TV's (20) to London" 
       android:textAppearance="?android:attr/textAppearanceSmall" /> 
     </LinearLayout> 

EDIT 2

我現在已經找到了一半的工作代碼,這裏的問題是圖像的寬度爲原點的寬度,而不是新的規模witdh。

 <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" 
      android:id="@+id/packet_list_view_layout_subvehicle" 
     > 
      <ImageView 
       android:id="@+id/packet_list_view_layout_subvehicle_imageAA" 
       android:layout_width="wrap_content" 
       android:layout_height="fill_parent" 
       android:src="@drawable/packet" 
       /> 

      <TextView 
       android:id="@+id/packet_list_view_layout_subvehicle_text" 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:gravity="center|left" 
       android:text="TV's (20) to London" 
       android:textAppearance="?android:attr/textAppearanceSmall" /> 
     </LinearLayout> 

回答

0

我終於找到了一種工作方式,不是很好,但爲我工作。

我的代碼現在

 <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" 
      android:id="@+id/packet_list_view_layout_subvehicle" 
     > 
      <mypacket.MyImageView 
       android:id="@+id/packet_list_view_layout_subvehicle_imageAA" 
       android:layout_width="wrap_content" 
       android:layout_height="fill_parent" 
       android:src="@drawable/packet" 
       /> 

      <TextView 
       android:id="@+id/packet_list_view_layout_subvehicle_text" 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:gravity="center|left" 
       android:text="TV's (20) to London" 
       android:textAppearance="?android:attr/textAppearanceSmall" /> 
     </LinearLayout> 

而且mypacket.MyImageView是此代碼。

public class MyImageView extends ImageView { 

    public MyImageView(Context context) { 
     super(context); 
    } 
    public MyImageView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 
    public MyImageView(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 

    } 
    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     super.onMeasure(heightMeasureSpec, heightMeasureSpec); 
    } 
} 

編輯:在真正的,我不使用此,基地之一的一些錯誤與系統如何calc下的大小,即做形象會更大然後需要,而這也將這樣做文字翹曲成兩條線。在最後我使用一個正常的ImageView並設置修正hegth和withd。