我有一个继承自LinearLayout的布局,我在MainActivity上有这个自定义视图。
当我将 自定义视图 放在 OnLongTouch 无效> 活动 ,我的 自定义视图 似乎 无法点击 !
布局代码:
public class AliLayout extends LinearLayout {
public AliLayout(Context context, AttributeSet attrs) {
super(context, attrs);
if(! isInEditMode()){
final AliLayout ly2 = this;//(AliLayout)findViewById(R.layout.ali_layout);
ly2.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View arg0) {
// TODO Auto-generated method stub
ly2.setTitle("Hello world!");
return false;
}
});
}
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
LayoutInflater.from(this.getContext()).inflate(R.layout.ali_layout,
this);
}
public void setTitle(String txt) {
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText(txt);
}
}
虚增xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/layout1"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="68dp"
android:layout_marginLeft="40dp"
android:background="#E0D641"
android:clickable="true"
android:longClickable="true"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
MainActivity:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:longClickable="true"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<com.example.component.AliLayout
android:id="@+id/ali1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="22dp" >
</com.example.component.AliLayout>
</RelativeLayout>
答案 0 :(得分:1)
尝试并失败了几百次后,我找到了解决问题的方法!
而不是:
ly2.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View arg0) {
// TODO Auto-generated method stub
ly2.setTitle("Hello world!");
return false;
}
});
我应该写:
this.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View arg0) {
// TODO Auto-generated method stub
setTitle("Hello World");
return true;
}
});
我不应该使用android:clickable
和android:longClickable
来扩充XML(ali_layout.xml):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/layout1"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="68dp"
android:layout_marginLeft="40dp"
android:background="#E0D641"
android:orientation="vertical" >
最后,问题解决了......
答案 1 :(得分:0)
问题是你的布局根本没有填满屏幕,因为你使用的是wrap_content而没有内容:
将布局切换为
<com.example.component.AliLayout
android:id="@+id/ali1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_marginTop="22dp" >
</com.example.component.AliLayout>
答案 2 :(得分:0)
我正在回答标题问题,您可以使用xml和代码的两个选项:
放入xml:
android:clickable="true"
或者在.java中
yourLinearLayout.setClickable(true);
答案 3 :(得分:0)
layout.setclickble="true"
和
layout.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View arg0) {
// TODO Auto-generated method stub
}
});