如何在某些事件上的textview上添加图像
在我的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();
}
});
现在我在此头中,向上和向下的箭头以显示排序要两个图像是按升序或降序。
请建议如何添加这两个图像。
谢谢。
您可以设置一个向上或向下箭头在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。
为什么你不把这些图像放在包含textviews的页眉布局xml文件中? – user370305
user370305:我需要更改onClick事件上的图像。并没有得到如何把它们放在xml文件 – Romi
我认为你必须把imageview放在你的页面布局xml文件中,并用任何一个图像来启动它,以提升和活动onClick的Imageview只是切换图像(降序),还有功能。 – user370305