getBaseline of TextView returns -1

天涯浪子 提交于 2019-12-12 04:22:48

问题


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

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

Yes, the parent view of TextView, LinerLayout has android:baselineAligned="true"


Edit : Adding Code

Layout

        <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>

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();

}

回答1:


View is not measured when onViewAttachedToWindow is called, hence it is returning -1.

Better approach would be.

@Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position)
    {
        .
        .
        .
        viewHolder.itemView.post(new Runnable()
                {

                   //put your code here


来源:https://stackoverflow.com/questions/38508542/getbaseline-of-textview-returns-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!