TextView的getBaseline返回-1 [英] getBaseline of TextView returns -1

查看:105
本文介绍了TextView的getBaseline返回-1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将两个TextView对齐,一个在HorizontalScrollView内,两个都是LinerLayout的子级.

I am trying to align two TextView one is inside HorizontalScrollView, and both are children of LinerLayout .

当我尝试在RecyclerView Adapter的onWindowAttached内调用TextViewgetBaseLine时,它总是返回-1.

When I am trying to call getBaseLine of TextView inside onWindowAttached of RecyclerView Adapter it always returns -1.

是的,TextViewLinerLayout的父视图具有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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆