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