我在RecyclerView
中遇到了一个视觉错误,直到我发现它是由RecyclerView
放入ConstraintLayout
引起的。
这是我尝试实现的目标(RecyclerView
GridLayoutManager
内LinearLayout
}:
这是我的第一次尝试(RecyclerView
GridLayoutManager
ConstraintLayout
内的RecyclerView
:
所以我的问题是:当RecyclerView
生活在ConstraintLayout
内时,为什么RecyclerView
项目无法正常使用?
使用布局检查器查看会产生wrap_content
覆盖整个屏幕宽度。
此外,在为项目的match_parent
和android:layout_width
分配android:layout_height
或wrap_content
时,这些项目按预期展开。
例如,当两者都设置为ConstraintLayout
时,这是package com.example.recyclerviewtest;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView recyclerView = findViewById(R.id.recyclerView);
recyclerView.setAdapter(new MyRecyclerViewAdapter());
}
}
中的结果:
这是破损布局的来源(第二个截图)。
MainActivity.java
package com.example.recyclerviewtest;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class MyRecyclerViewAdapter extends RecyclerView.Adapter {
class ViewHolder extends RecyclerView.ViewHolder {
ViewHolder(View itemView) {
super(itemView);
}
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View view = layoutInflater.inflate(R.layout.recycler_view_item, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
}
@Override
public int getItemCount() {
return 3;
}
}
MyRecyclerViewAdapter.java
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layoutManager="GridLayoutManager"
app:spanCount="2" />
</android.support.constraint.ConstraintLayout>
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:background="@android:color/holo_green_light"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="LINE 1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="LINE 2" />
</LinearLayout>
recycler_view_item.xml
android:layout_width
总结:使用android:layout_height
时,在将固定值设置为ConstraintLayout
或import numpy as np
import cv2
gray = cv2.imread('image.png')
edges = cv2.Canny(gray,50,150,apertureSize = 3)
cv2.imwrite('edges-50-150.jpg',edges)
minLineLength=100
lines = cv2.HoughLinesP(image=edges,rho=1,theta=np.pi/180, threshold=100,lines=np.array([]), minLineLength=minLineLength,maxLineGap=80)
a,b,c = lines.shape
for i in range(a):
x = lines[i][0][0] - lines [i][0][2]
y = lines[i][0][1] - lines [i][0][3]
if x!= 0:
if abs(y/x) <1:
cv2.line(gray, (lines[i][0][0], lines[i][0][1]), (lines[i][0][2], lines[i][0][3]), (255, 255, 255), 1, cv2.LINE_AA)
se = cv2.getStructuringElement(cv2.MORPH_ELLIPSE , (3,3))
gray = cv2.morphologyEx(gray, cv2.MORPH_CLOSE, se)
cv2.imwrite('houghlines.jpg', gray)
cv2.imshow('img', gray)
cv2.waitKey(0)
时,布局才会中断。
答案 0 :(得分:1)
尝试将您的回收者视图宽度设置为0dp,添加一个约束startToStarOf父级和一个约束endToEndOf父级。另外,将对比度horizontalWeight添加到1。
让我知道它有效。