Android 将视图动态添加到具有换行内容高度的线性布局不会';我什么也看不出来
我正在尝试使用Android 将视图动态添加到具有换行内容高度的线性布局不会';我什么也看不出来,android,android-layout,android-linearlayout,Android,Android Layout,Android Linearlayout,我正在尝试使用wrap\u内容height向LinearLayout动态添加视图。但是,除非我添加定义的dp高度,例如300dp,否则它不起作用。为什么我不能以编程方式添加视图并让父级LinearLayout包装内容 我试图添加的视图: public class ImageCanvas extends View { private static final String TAG = "ImageCanvas"; private Bitmap icon;
wrap\u内容
height向LinearLayout
动态添加视图。但是,除非我添加定义的dp
高度,例如300dp
,否则它不起作用。为什么我不能以编程方式添加视图并让父级LinearLayout
包装内容
我试图添加的视图:
public class ImageCanvas extends View {
private static final String TAG = "ImageCanvas";
private Bitmap icon;
public ImageCanvas(Context context) {
super(context);
Drawable drawable = ContextCompat.getDrawable(context, R.drawable.piggy);
if(drawable != null){
icon = ((BitmapDrawable) drawable).getBitmap();
}
Log.d(TAG, "ImageCanvas: " + icon);
}
public ImageCanvas(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public ImageCanvas(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawBitmap(icon, 0, 0, null);
}
}
LinearLayout
我正在尝试将上述视图添加到:
<LinearLayout
android:id="@+id/drawingPad"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@+id/btn_create_image"
app:layout_constraintBottom_toBottomOf="parent"
android:paddingBottom="32dp"
android:orientation="vertical"
android:paddingTop="32dp"/>
当我将XML LinearLayout设置为定义的高度(如200dp
)时,它可以工作,但它不适用于wrap\u content
,如何才能使它适用于wrap\u content
\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
更新1:
我将所有内容简化为XML布局,结果发现问题是父级NestedScrollView
<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView 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">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/parentLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/btn_create_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:text="Create image"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<LinearLayout
android:id="@+id/drawingPad"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="32dp"
android:paddingBottom="32dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btn_create_image" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>
这是整个布局。当您添加
NestedScrollView
并尝试以编程方式添加视图时,位图将不会在屏幕上绘制。当您删除NestedScrollView
时,将显示位图。如果有人知道如何使用NestedScrollView
进行此操作,请分享 问题可能在于约束布局。
对于约束布局>=1.1
app:layout_constrainedHeight=”true"
android:layout_height="wrap_content"
对于约束布局,您需要使用
onMeasure()提供ImageCanvas的测量值。
覆盖:
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(icon.getWidth(),icon.getHeight());
}
看
明确提供高度时,该高度将在自定义构件中设置。对于包裹内容,高度将设置为零,因为它既没有指定也没有明确测量。上述代码将提供正确的测量值
(icon.getHeight()
是您真正需要的,对应于wrap\u content
。根据您的设计,icon.getWidth()
可能需要不同的值。)
不确定NestedScrollView为什么会有任何效果,但上面的代码会有所帮助。什么是“小猪”绘图?你能把它寄出去吗?这为什么在这种情况下,可绘制性很重要?可绘制性可能是不寻常的,但jpg非常简单。您可能会遇到约束问题,而不是可提取问题。无论如何,我把你的代码放在一个空白的项目中,它可以与包装内容一起工作。如果你被难住了,没有得到一个好的答案,一个会有帮助的。@Cheticamp是的,你是对的。这不是代码的问题。在我将所有内容简化为一个最小的示例后,将
NestedScrollView
作为根布局是一个问题,这导致位图根本不显示(更新了上面的完整布局)。仍在尝试使用NestedScrollView
Hey感谢您的回复!我将所有内容简化为一个非常简单的布局(上面有问题的更新),结果是将NestedScrollView
作为父视图是一个问题。我尝试将您的解决方案添加到约束布局中的xml中,但没有帮助。我尝试了您的答案,但它似乎仍然没有显示位图。这是我的完整代码,在添加NestedScrollView
作为根视图之前,一切都正常。删除它,它就可以很好地显示位图。@DIRTYDAVE我被你的pastebin代码弄糊涂了。它甚至没有线性布局,drawingPad是一个图像视图,而不是像问题中那样的线性布局。对不起,我不得不对它进行一些更改,因为我无法将其设置为work@DIRTYDAVE我理解,但这个问题似乎没有反映当前的情况。我在pastebin中也没有看到onMeasure()代码。另一方面,我还将取出ConstraintLayout的直接子级中的所有match_父维度,并将其替换为带有适当约束的0dp
。match_parent在ConstraintLayout中确实是非法的,尽管它没有lint检查。match_parent可能会导致奇怪的问题……我知道这是疯狂的随机,但在我将android:fillViewport=“true”
添加到NestedScrollView
后,位图将加载。我正要放弃
android:layout_height="wrap_content"
app:layout_constraintHeight_default="wrap"
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(icon.getWidth(),icon.getHeight());
}