【发布时间】:2019-03-25 04:03:41
【问题描述】:
我有一个自定义的 ConstraintLayout 类 (Card.java),它覆盖了 onDraw() 方法以在他的背景中绘制一个六边形。在前台,我尝试使用三个TextViews 来显示三个数字。
为此,我在 Card 的构造函数中填充了 card.xml。 TextViews 显示,但不在正确的位置。它们应该与卡片的宽度和高度相匹配,然后将自身定位到卡片的左上角和右上角以及卡片的底部。但是他们会做一些事情,比如缩小自己并转到左上角。
我尝试将 card.xml 的根元素更改为“merge”而不是“...ConstraintLayout”,但这并没有改变任何东西。
我还尝试使用指南来相对于其宽度定位 TextView。我尽量避免使用固定边距,所以文本总是在正确的位置,当卡片的大小发生变化时也是如此。
Card.java:
public class Card extends ConstraintLayout {
private int mSize;
private Path mHexagon;
private Paint mPaintHexagon;
private TextView mT1, mT2, mT3;
public Card(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
inflate(context, R.layout.card, this);
// Numbers
mT1 = findViewById(R.id.num1);
mT2 = findViewById(R.id.num2);
mT3 = findViewById(R.id.num3);
// Hexagon
mSize = Field.getHexSize(); // Size is used to calculate the
setPath();
mPaintHexagon = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaintHexagon.setColor(0x50888888);
mPaintHexagon.setStyle(Paint.Style.FILL);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawPath(mHexagon, mPaintHexagon);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int width = 2 * mSize;
int height = (int) (Math.sqrt(3) * mSize);
Log.d(TAG, "Measure w:" + width + " h:" + height);
setMeasuredDimension(width, height);
}
}
card.xml:
<?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"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/num2"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
android:text="2"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="8dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/num1"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
android:text="1"
app:layout_constraintStart_toEndOf="@+id/num2"
app:layout_constraintEnd_toStartOf="@+id/num3"
app:layout_constraintBottom_toBottomOf="parent"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/num3"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
android:text="3"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="8dp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"/>
</android.support.constraint.ConstraintLayout>
activity_main.xml:
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:background="@color/colorAccentDark"
android:padding="5dp">
<de.portugall.markus.takeiteasy.Card
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:id="@+id/card"/>
</android.support.constraint.ConstraintLayout>
【问题讨论】:
-
@MikeM。如上所述,我已经尝试使用
<merge>而不是<ConstraintLayout>。我添加了一张图片,您可以在其中看到使用<merge>时卡片的外观。 -
你没有说
Card.java是如何添加到布局中的。如果您以编程方式将卡添加到ConstraintLayout中,您是否设置了它的约束?如果你不这样做,一切都会崩溃到(0,0)。发布更多关于如何将Card.java添加到视图层次结构中的信息以获得更多帮助。 -
糟糕,抱歉,我错过了那部分。好吧,你肯定想要
<merge>标签,因为否则你最终会得到一个额外的嵌套ConstraintLayout。除此之外,我不使用ConstraintLayout,所以我不确定我能提供更多帮助。对于那个很抱歉。不过,我会说,ConstraintLayout对于这样一个相对简单的布局可能有点矫枉过正。如果你不是绝对需要它成为ConstraintLayout,你可以考虑尝试更直接的ViewGroup,比如RelativeLayout。 Cheticamp 提出了一个很好的观点。 -
@MikeM。我使用了 ConstraintLayout,因为我可以在指南的帮助下使用百分比边距。如果我可以制作一个具有百分比边距的 RelativeLayout,我会使用它,但我读到如果你需要类似的东西,ConstrainLayout 会更好。
-
@Cheticamp 我编辑了帖子,以便您可以看到我的
activity_main.xml,它通常只是添加到MainAcivity。所以Card只是添加到 xml 中。我只是玩了一下它的约束,我注意到,当我将它设置为0dp时,完整的布局将与其父级匹配,并且TextViews 也显示在右上角。但我想知道为什么当我将约束设置为wrap_content时,文本比背景中的六边形更包裹
标签: java android android-layout android-custom-view