2011-11-22 80 views
1

在我的android應用程序中,我有一個列表和一個列表標題。爲表頭,我用這個佈局:如何在某些事件上的textview上添加圖像

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" android:layout_height="?android:attr/listPreferredItemHeight" 
    android:weightSum="1"> 

    <TextView android:text="#" android:layout_height="wrap_content" 
     android:layout_width="0dp" android:background="@drawable/back" 
     android:layout_weight=".05" android:gravity="center_horizontal" 
     android:id="@+id/header_num"/>: 
    <TextView android:text="Summary" android:layout_height="wrap_content" 
     android:layout_width="0dp" android:background="@drawable/back" 
     android:layout_weight=".55" android:gravity="center_horizontal" 
     android:id="@+id/header_summ" /> 
</LinearLayout> 

爲Android:背景=「@繪製/後退」:

<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle"> 
    <solid android:color="#ffffff" /> 
    <stroke android:width="1dip" android:color="#4fa5d5" /> 


</shape> 

現在頭項目的基礎上,正在申請排序列表項爲:

ticketnum = (TextView) findViewById(R.id.header_num); 
     ticketnum.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       Comparator<Ticket> mc; 
       mc = new TicketIdComparator(); 
       Collections.sort(tickets, mc); 
       System.out.println(tickets); 
       ticket_adapter = new TicketAdapter(getApplicationContext(), 
         R.layout.ticket_details_row, tickets); 
       setListAdapter(ticket_adapter); 

       ticket_adapter.notifyDataSetChanged(); 

      } 
     }); 

現在我在此頭中,向上和向下的箭頭以顯示排序要兩個圖像是按升序或降序。

請建議如何添加這兩個圖像。

謝謝。

+0

爲什麼你不把這些圖像放在包含textviews的頁眉佈局xml文件中? – user370305

+0

user370305:我需要更改onClick事件上的圖像。並沒有得到如何把它們放在xml文件 – Romi

+0

我認爲你必須把imageview放在你的頁面佈局xml文件中,並用任何一個圖像來啓動它,以提升和活動onClick的Imageview只是切換圖像(降序),還有功能。 – user370305

回答

1

您可以設置一個向上或向下箭頭在TextView的文本的左側(以使它看起來像一個圖標...我認爲這是你想要的嗎?)

首先在刪除一個PNG您可繪製文件夾,並把它upArrow.png,並添加一個名爲downArrow.png

然後設置箭頭以顯示爲一個TextView文本的左側:

myTextView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.upArrow, 0, 0, 0); 

setCompoundDrawablesWithIntrinsicBounds(int left, int top, int right, int bottom)使用其他參數t上方,右方或下方的箭頭。

現在,如果您想要更改背景圖片(例如切換出R.drawable.back),那麼您還需要先在drawable文件夾中保存upArrow.png和downArrow.png,然後調用

myTextView.setBackgroundDrawable(R.drawable.upArrow); 

setBackgroundDrawble(Drawable d)

另外,還要注意(自2.1),你可以把這個方法調用你的onclick監聽器在你的XML。這可以使代碼更清晰。在你情況下一個onclick listenr添加到headerNum:

<TextView android:text="#" android:layout_height="wrap_content" 
     android:layout_width="0dp" android:background="@drawable/back" 
     android:layout_weight=".05" android:gravity="center_horizontal" 
     android:id="@+id/header_num" 
     android:onClick="myOnclickListener"/> 

然後在你的Activity類簡單的添加方法:

public void myOnclickListener(View v) { 
    ... 
} 

你也可以以編程方式添加一個ImageView的,雖然這是更多地參與。或者你可以在長達兩個imageViews和向下箭頭每頭旁邊,打開和關閉添加,並打開能見度爲適當的一次編程方式使用:

myImageView.setVisibility(View.VISIBLE); 

myImageView.setVisibility(View.GONE); 

雖然我認爲使用bg圖像或左邊的drawables是最簡單的,並保持你的xml最小化。如果你真的添加imageViews,你可能會想切換到RelativeLayout。

+0

彼得:myImageView是?? – Romi

+0

@Romi - 這是您用來顯示箭頭的imageView。 - 你可以把這個ImageView放在原始的XML中<'。您可以爲每個textView放置一個ImageView,並使用setBackgroundDrawable更改您顯示的箭頭。或者每個TextView有2個ImageViews,並切換它們的可見性....我認爲使用leftDrawable(我概述的第一個解決方案)會更容易。 –

+0

是的...第一個解決方案最適合我的要求。因爲我在運行時想要這兩個圖像。 – Romi