gpt4 book ai didi

具有正确 layout_gravity 的 Android Horizo​​ntalScrollView 工作错误

转载 作者:可可西里 更新时间:2023-11-01 19:07:35 24 4
gpt4 key购买 nike

我正在尝试获得以下 View :左侧站点的描述和右侧的值。例如:

enter image description here


问题是文本可能很长,所以我将它包装在 Horizo​​ntalScrollView 中。我正在使用以下 xml:

<LinearLayout
android:layout_width="fill_parent"
android:layout_weight="1"
android:orientation="horizontal"
android:layout_height="wrap_content">
<TextView
android:layout_height="wrap_content"
android:textColor="#000000"
android:layout_width="110dp"
android:text="Description:"/>
<HorizontalScrollView
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_height="wrap_content"
android:textColor="#000000"
android:layout_width="wrap_content"
android:layout_gravity="right"
android:text="Very1 very2 very3 very4 very5 very6 long text"/>
</HorizontalScrollView>
</LinearLayout>

这就是问题所在。在滚动到最左边的角落后,我看到了(缺少文本开头):

enter image description here


滚动到最右边的角落后,我看到了 View (文本后的添加空间):

enter image description here


如果我将 TextView android:layout_gravity 更改为“left”,一切都会按预期进行。滚动到最左边的角落后,我看到了:

enter image description here


滚动到最右边的角落后,我得到了 View :

enter image description here


这是一种使其正常工作并在短文本时将文本对齐到正确站点的方法吗?

最佳答案

我知道这已经有一段时间了,但这里有一个解决方法(已测试):

public class RightAlignedHorizontalScrollView extends HorizontalScrollView {
private boolean mGravityRight = false;
private boolean mAutoScrolling = false;

public RightAlignedHorizontalScrollView(Context context) {
super(context);
}

public RightAlignedHorizontalScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}

public RightAlignedHorizontalScrollView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}

public View getChildView() {
return getChildAt(0);
}

private int getScrollRange() {
int scrollRange = 0;
if (getChildCount() > 0) {
View child = getChildAt(0);
scrollRange = Math.max(0, child.getWidth() - (getWidth() - getPaddingLeft() - getPaddingRight()));
}
return scrollRange;
}

@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
// HorizontalScrollView is broken for Gravity.RIGHT. So we're fixing it.
mAutoScrolling = false;
int childWidth = getChildView().getWidth();
super.onLayout(changed, left, top, right, bottom);
int delta = getChildView().getWidth() - childWidth;
View childView = getChildView();
FrameLayout.LayoutParams p = (LayoutParams) childView.getLayoutParams();
int horizontalGravity = p.gravity & Gravity.HORIZONTAL_GRAVITY_MASK;
int verticalGravity = p.gravity & Gravity.VERTICAL_GRAVITY_MASK;
if (horizontalGravity == Gravity.RIGHT) {
if (getScrollRange() > 0) {
mGravityRight = true;
p.gravity = Gravity.LEFT | verticalGravity;
childView.setLayoutParams(p);
super.onLayout(changed, left, top, right, bottom);
}
} else if (mGravityRight) {
if (getScrollRange() == 0) {
mGravityRight = false;
p.gravity = Gravity.RIGHT | verticalGravity;
childView.setLayoutParams(p);
super.onLayout(changed, left, top, right, bottom);
}
}
if (mGravityRight && delta > 0) {
scrollBy(delta, 0);
mAutoScrolling = true;
}
}

@Override
public void computeScroll() {
if (mAutoScrolling) return;
super.computeScroll();
}

@Override
public void scrollTo(int x, int y) {
if (mAutoScrolling) return;
super.scrollTo(x, y);
}

关于具有正确 layout_gravity 的 Android Horizo​​ntalScrollView 工作错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9031817/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com