- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我在 ConstraintLayout
中使用 RecyclerView
时遇到问题,我想在将其声明为错误之前确认我没有做错任何事情。基本上我在 ConstraintLayout
中定义了一个 RecyclerView
:
<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:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:id="@+id/list"/>
</android.support.constraint.ConstraintLayout>
我已经定义了它的项目,它是 ConstraintLayout
(或任何其他 ViewGroup
)中的 TextView
:
<android.support.constraint.ConstraintLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="120dp"
android:layout_margin="8dp"
android:background="@color/colorAccent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello" />
</android.support.constraint.ConstraintLayout>
在使用一个简单的适配器将一些数据绑定(bind)到这个 RecyclerView 之后,输出如下所示:
它似乎在下一个测量周期中就正确了,因为向下滚动:
然后向上滚动修复前两个项目,我认为它们的宽度已重新计算:
项目周围的 ConstraintLayout
- 它应该占据 View 的整个宽度 - 而不是 wrap_content
。如果此 ConstraintLayout
的高度设置为 wrap_content
,问题就会消失。如果删除 TextView
,问题就会消失。最后,如果 RecyclerView
没有包含在 ConstraintLayout
中,问题就会消失。
这是适配器:
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private String[] mDataset;
public static class ViewHolder extends RecyclerView.ViewHolder {
public ViewGroup vg;
public ViewHolder(ViewGroup v) {
super(v);
vg = v;
}
}
public MyAdapter(String[] myDataset) {
mDataset = myDataset;
}
@Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
ViewGroup v = (ViewGroup) LayoutInflater.from(parent.getContext())
.inflate(R.layout.simple_item, parent, false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
// Do nothing
}
@Override
public int getItemCount() {
return mDataset.length;
}
}
这里是build.gradle
:
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
buildToolsVersion "27.0.3"
defaultConfig {
applicationId "com.ibsplc.icargo.icoandroid.recyclerviewbugtest"
minSdkVersion 21
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:26'
compile 'com.android.support:design:26.0.0'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.android.support:recyclerview-v7:26.0.0'
testCompile 'junit:junit:4.12'
}
Here是一个完整的例子。
最佳答案
如 ConstraintLayout
的文档中所述,您不应为其子级使用 match_parent
:
Important: MATCH_PARENT is not recommended for widgets contained in a ConstraintLayout. Similar behavior can be defined by using MATCH_CONSTRAINT with the corresponding left/right or top/bottom constraints being set to "parent"
RecyclerView
的正确约束应如下所示:
<android.support.v7.widget.RecyclerView
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:id="@+id/list"/>
您还应该通过设置至少一个垂直约束和一个水平约束来约束项目布局中的 TextView
:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
最后但同样重要的是,您可能应该使用最新版本的 ConstraintLayout
:'com.android.support.constraint:constraint-layout:1.1.2'
而不是 1.0.2。
关于android - ConstraintLayout 内的 RecyclerView 项目不占据其父级的整个宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51347903/
我正在开发一个 Android 库,这个库有一些布局 Activity 。 在布局中,我有下一个代码 ... Make sure you have correct i
我想以编程方式将一个 ConstraintLayout 放入另一个 ConstraintLayout 中。但是,如果我按照之前使用 RelativeLayout 的方式对其进行膨胀,它不会显示。 这是
我在我的布局中使用 androidx.constraintlayout.widget.ConstraintLayout 并且它不显示链,我也不能拖动任何小部件。我只需键入要使用的约束。 这是 and
我注意到我可以注释掉 implementation 'androidx.constraintlayout:constraintlayout:2.0.4'尽管我在我的应用程序中使用了 Constrain
我尝试以编程方式添加 ConstraintLayout。它在工作,但 WRAP_CONTENT 不工作。布局始终为 MATCH_PARENT。 Android 开发者页面没有为 ConstraintL
使用 Android Studio 菜单选项 Refactor -> Refactor to Androidx...迁移到 Androidx 包后... 我收到以下错误: Error inflatin
我正在尝试学习 Android 开发,但问题是我的应用程序每次尝试运行时都会崩溃,并且在 logcat 中出现以下错误: 2019-06-30 16:17:18.198 5317-5317/? E/e
我刚刚通过 Android Studio 菜单选项 重构 -> 重构到 AndroidX 迁移到 androidx 我收到以下错误: android.view.InflateException: Bi
我遇到一个错误,ConstraintLayout 中存在空对象引用 Logcat E/UncaughtException: java.lang.NullPointerException: Attemp
如问题标题所示,我已经搜索了我的问题的答案,并找到了 Error inflating class androidx.constraintlayout.ConstraintLayout after mi
因此,我更新了我的 gradle 文件,我的应用开始崩溃并出现一些奇怪的错误。它开始莫名其妙地崩溃,而且我的 java 类中的抽屉导航代码也出现了错误,这是我以前没有遇到过的。 我已经尝试使用我的版本
当我尝试使用 XML 在覆盖中插入 Compose(在其他应用程序上绘制)时,我得到了这个异常: java.lang.IllegalStateException: ViewTreeLifecycleO
我正在尝试通过 CodingInFlow 在 YouTube 上关注 Android Studio 开发的播放列表。 我正在尝试关注此视频,但遇到了问题... https://www.youtube.
在android studio SDK工具中有两个ConstraintLayout选项,一个是ConstraintLayout for android,另一个是Solver for Constrain
我有一个非常令人沮丧的错误,我无法解释。我创建了一个 Android 应用程序,它使用 Androidx AppCompat 来使其与旧版本兼容。这是我的主要 Activity 布局文件:{Andro
免责声明:我知道 Compose 刚刚进入 alpha01,因此我不希望每个功能可用。但是,特定布局案例的布局和处理是恕我直言的一个重要主题应该尽早解决 😉。 当前基于 View Constrain
Attempt to invoke virtual method 'boolean androidx.constraintlayout.solver.widgets.ConstraintWidgetC
我正在尝试自己学习android开发,因为我试图使用按钮和 TextView 制作一个简单的文本更改应用程序,但是当使用USB在我自己的手机上运行它时,不幸的是,它开始显示该应用程序已经停止。如果您不
我遇到了一个奇怪的问题:layout.xml 我正在尝试将一个小部件连接到我的屏障。问题是,当我尝试将我的小部件的 END 与 barrier 连接时,没有
所以我有一个 RecycleView 的布局文件,它具有可展开/可折叠的 View 。 点击标题会展开/折叠额外的数据。在编辑器中一切看起来都很好。但是,它会有一个类似于 layout_marginB
我是一名优秀的程序员,十分优秀!