【发布时间】:2018-05-12 00:46:12
【问题描述】:
我的问题是我有一个LinearLayout,我在运行时将它膨胀到ScrollView 内的LinearLayout。
这是main_activity.xml
<ScrollView
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/controlLayoutCV"
android:layout_alignParentStart="true"
android:layout_below="@+id/toolLayoutCV">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="@dimen/dp5"
android:paddingLeft="@dimen/dp7"
android:paddingRight="@dimen/dp7"
android:paddingTop="@dimen/dp5">
<android.support.v7.widget.CardView
android:id="@+id/questionQuizCV"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardElevation="@dimen/dp2">
<com.emedicoz.app.CustomViews.JustifiedTextView
android:id="@+id/questionQuizTV"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/dp5"
android:text="" />
</android.support.v7.widget.CardView>
<LinearLayout
android:id="@+id/quizQuestionLL"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/questionQuizCV"
android:layout_marginTop="@dimen/dp5"
android:orientation="vertical"
android:padding="@dimen/dp5" />
</RelativeLayout>
</ScrollView>
这是item_layout.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mcqlayout_LL"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="@dimen/dp5"
android:minHeight="30dp"
android:orientation="horizontal"
android:paddingBottom="@dimen/dp7"
android:paddingTop="@dimen/dp7">
<TextView
android:id="@+id/optioniconTV"
android:layout_width="@dimen/dp40"
android:layout_height="@dimen/dp40"
android:background="@drawable/circle_bg"
android:gravity="center"
android:padding="@dimen/dp3"
android:text="A"
android:textSize="@dimen/sub_heading_text_size" />
<com.emedicoz.app.CustomViews.JustifiedTextView
android:id="@+id/optionTextTV"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="@dimen/dp10" />
</LinearLayout>
这是我为直接显示HTML 内容而创建的CustomTextView。 JustifiedTextView.class 是
public class JustifiedTextView extends WebView {
private String text = "";
private int textSize = 12;
private int backgroundColor = Color.TRANSPARENT;
public JustifiedTextView(Context context, AttributeSet attrs) {
super(context, attrs);
this.setWebChromeClient(new WebChromeClient() {});
}
public void setText(String s) {
this.text = s;
reloadData();
}
@SuppressLint("NewApi")
private void reloadData() {
// loadData(...) has a bug showing utf-8 correctly. That's why we need to set it first.
this.getSettings().setDefaultTextEncodingName("utf-8");
// this.loadData(String.format(core,textColor,textSize,text), "text/html","utf-8");
this.loadData(text, "text/html", "utf-8");
// set WebView's background color *after* data was loaded.
super.setBackgroundColor(backgroundColor);
// Hardware rendering breaks background color to work as expected.
// Need to use software renderer in that case.
if (android.os.Build.VERSION.SDK_INT >= 11)
this.setLayerType(WebView.LAYER_TYPE_SOFTWARE, null);
}
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int heightMeasureSpec_custom = MeasureSpec.makeMeasureSpec(
Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, heightMeasureSpec_custom);
ViewGroup.LayoutParams params = getLayoutParams();
params.height = getMeasuredHeight();
}
public void setTextSize(int textSize) {
this.textSize = textSize;
reloadData();
}
}
我已经尝试了下面提到的所有解决方案。
- Disable WebView touch events in Android
- 已尝试将
android:descendantFocusability="blocksDescendants"设置为ScrollView
为什么点击WebView时LinearLayout的点击事件没有触发?
这就是我膨胀Layout 并处理点击事件的方式。
private LinearLayout initAnswerMCViews(String text, String questions, Questions questionsModel) {
LinearLayout view = (LinearLayout) View.inflate(activity, R.layout.mcq_quiz, null);
answerTV = (JustifiedTextView) view.findViewById(R.id.optionTextTV);
optionIconTV = (TextView) view.findViewById(R.id.optioniconTV);
mcqItemLL = (LinearLayout) view.findViewById(R.id.mcqlayout_LL);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
lp.setMargins(3, 3, 3, 3);
mcqItemLL.setLayoutParams(lp);
if (questionsModel.isAnswered()) {
String[] answer = questionsModel.getUser_answer().split(",");
for (int i = 0; i < answer.length; i++) {
if (answer[i].equals(text)) {
answerTV.setText(questions);
optionIconTV.setText(text);
optionIconTV.setBackgroundResource(R.drawable.circle_bg_true);
} else {
answerTV.setText(questions);
optionIconTV.setText(text);
}
}
} else {
answerTV.setText(questions);
optionIconTV.setText(text);
}
mcqItemLL.setTag(R.id.questions, optionIconTV.getText().toString());
mcqItemLL.setTag(R.id.optionsAns, mcqItemLL);
mcqItemLL.setOnClickListener(optionClickListener);
viewArrayList.add(mcqItemLL);
return view;
}
为什么单击Layout 中的WebView 部分时单击不被监听?
【问题讨论】:
-
尝试设置生成视图的可点击属性(通过 setClickable(boolean))。将您的 LinearLayout 设置为不可点击,将子视图设置为可点击
-
我需要使可点击的主要父布局。所以我不能这样做
-
哦,我误解了你的问题。您想调用 LinearLayout 的 onClick() 而不是您孩子的 onClick(),对吗?在这种情况下,您可以做相反的事情:将子项设置为不可点击,将 LinearLayout 设置为可点击。如果您也需要孩子可以点击,您可以使用 onTouchListener,检测点击(使用 GestureDetector)并返回 true 以指示点击事件已在 LinearLayout 的 onTouch() 中消耗。
-
@GabrielCosta 已经通过在两个 TextView 中启用 clickable false 来实现,但它仍然无法正常工作。
-
我相信 LinearLayout 的默认可点击是 false。那么,您是否尝试将 clickable=true 添加到您的 LinearLayout 中?尝试将“clickable=true”添加到 LinearLayouts(quizQuestionLL 和 mcqlayout_LL)
标签: android android-layout webview scrollview onclicklistener