我有一个自定义窗口小部件,可以扩展线性布局。它只是带有litle的文本块。
这是小部件的xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:background="@drawable/actionbar_background"
android:layout_height="35dp"
android:paddingLeft="15dp"
android:gravity="center_vertical">
<TextView
android:id="@+id/title"
android:layout_height="35dp"
android:textColor="@color/white"
android:layout_width="match_parent"
android:textSize="18dp"
android:clickable="true"
android:gravity="center_vertical" />
</LinearLayout>
<TextView
android:id="@+id/ingreds"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="Some text"
android:paddingLeft="15dp" />
</LinearLayout>
如果我onClickListener
在其中的某个位置为该自定义窗口小部件创建Activity
-仅当我单击TextView
该布局中的最后一个时,它才会做出反应。但是我需要将onClickListener设置为LinearLayout
包含“标题” textView的水平,或者直接在标题TextView上设置。重要提示:我想设置这些听众OUTSIDE自定义Widget类。
我认为我需要setOnclickListener
在自定义类中重写方法或类似的方法,但是我不确定该如何完成。
这是自定义窗口小部件的类:
public class MyTextBlock extends LinearLayout {
private TextView title;
private TextView ingreds;
public MyTextBlock(Context context) {
this(context, null);
}
public MyTextBlock(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.my_text_block, this, true);
title = (TextView) findViewById(R.id.title);
ingreds = (TextView) findViewById(R.id.ingreds);
}
public void setText(String text) {
ingreds.setText(text);
}
public void setTitle(String titleText) {
title.setText(titleText);
}
}
谢谢回答。
只需将OnClickListener实现到此视图类并进行设置 textview.setOnClickLister(this);
现在处理onClick(View v)
方法下的click事件。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句