gpt4 book ai didi

android - 如何使 LinearLayouts 可点击

转载 作者:行者123 更新时间:2023-11-30 02:57:09 25 4
gpt4 key购买 nike

我有一个继承自 LinearLayout 的布局,我在 MainActivity 上有这个自定义 View 。

我的问题是,当我将此自定义 View 放在 上时,OnLongTouch 将不起作用> Activity ,我的自定义 View 似乎不可点击 !

布局代码:

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>

主要 Activity :

<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>

最佳答案

在尝试和失败一百次之后,我找到了解决问题的方法!

代替:

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:clickableandroid: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" >

终于,问题解决了...

关于android - 如何使 LinearLayouts 可点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23091564/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com