TextView的getBaseline返回-1 [英] getBaseline of TextView returns -1
问题描述
我试图将两个TextView
对齐,一个在HorizontalScrollView
内,两个都是LinerLayout
的子级.
I am trying to align two TextView
one is inside HorizontalScrollView
, and both are children of LinerLayout
.
当我尝试在RecyclerView Adapter的onWindowAttached
内调用TextView
的getBaseLine
时,它总是返回-1.
When I am trying to call getBaseLine
of TextView
inside onWindowAttached
of RecyclerView Adapter it always returns -1.
是的,TextView
,LinerLayout
的父视图具有android:baselineAligned="true"
Yes, the parent view of TextView
, LinerLayout
has android:baselineAligned="true"
添加代码
布局
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingBottom="6dp"
android:paddingEnd="10dp"
android:baselineAligned="true"
android:paddingStart="10dp"
android:paddingTop="10dp">
<TextView
android:id="@+id/outerTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical" />
<HorizontalScrollView
android:id="@+id/scrollView"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/inlineTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
/>
</HorizontalScrollView>
</LinearLayout>
RecyclerView适配器中的代码
Code in RecyclerView Adapter
@Override
public void onViewAttachedToWindow(RecyclerView.ViewHolder holder)
{
super.onViewAttachedToWindow(holder);
if(holder instanceof MyHolder)
{
dostuff((MyHolder)holder);
}
}
private void dostuff(QACommentViewHolder holder)
{
int baseline = holder.outerTextView.getBaseline();
}
推荐答案
View
在调用onViewAttachedToWindow
时未测量,因此返回-1.
View
is not measured when onViewAttachedToWindow
is called, hence it is returning -1.
更好的方法.
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position)
{
.
.
.
viewHolder.itemView.post(new Runnable()
{
//put your code here
这篇关于TextView的getBaseline返回-1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!