我正在尝试获取以下视图:左侧网站的说明和右侧的值。 e.g:
问题是文本可能很长,所以我将它包装在HorizontalScrollView中。我正在使用以下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>
这就是问题所在。滚动到最左边的角落后,我得到了视图(缺少文本的开头):
在滚动到最右边的角落后,我得到了视图(文字后的空格):
如果我将TextView android:layout_gravity更改为“left”,一切都按预期工作。滚动到最左边的角落后,我得到了视图:
在滚动到最右边的角落后,我得到了视图:
是它是一种使其正常工作并在文本缩短时将文本与正确的网站对齐的方法吗?
答案 0 :(得分:2)
我知道它已经有一段时间但是这是一种解决方法(已测试):
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);
}
}
答案 1 :(得分:1)
不要对TextView使用任何布局重力,它只会按预期工作。
正如你所说的那样,问题存在,它在正确的引力下行为错误。
答案 2 :(得分:1)
我知道这一年已经处于非活动状态,但在2013年仍然存在问题。创建一个扩展HorizontalScrollView并复制/粘贴下面代码的新类。
@Override
public boolean onTouchEvent(MotionEvent ev) {
autoScrolling = false;
return super.onTouchEvent(ev);
}
private int getScrollRange() {
int scrollRange = 0;
if(getChildCount() > 0) {
View child = getChildAt(0);
scrollRange = Math.max(0, child.getWidth() - (getWidth() - getPaddingLeft() - getPaddingRight()));
}
return scrollRange;
}
private boolean gravityRight = false;
private boolean autoScrolling = false;
@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.
if(getChildCount() == 0) return super.onLayout(changed, left, top, right, bottom);
int childWidth = getChildAt(0).getWidth();
super.onLayout(changed, left, top, right, bottom);
int delta = getChildAt(0).getWidth() - childWidth;
AdvancedDisplay view = getView();
ScrollableDisplay.LayoutParams p = (LayoutParams) view.getLayoutParams();
int horizontalGravity = p.gravity & Gravity.HORIZONTAL_GRAVITY_MASK;
int verticalGravity = p.gravity & Gravity.VERTICAL_GRAVITY_MASK;
if(horizontalGravity == Gravity.RIGHT) {
if(getScrollRange() > 0) {
gravityRight = true;
p.gravity = Gravity.LEFT | verticalGravity;
view.setLayoutParams(p);
super.onLayout(changed, left, top, right, bottom);
}
}
else if(gravityRight) {
if(getScrollRange() == 0) {
gravityRight = false;
p.gravity = Gravity.RIGHT | verticalGravity;
view.setLayoutParams(p);
super.onLayout(changed, left, top, right, bottom);
}
}
if(gravityRight && delta > 0) {
autoScrolling = false;
scrollBy(delta, 0);
autoScrolling = true;
}
}
@Override
public void computeScroll() {
if(autoScrolling) return;
super.computeScroll();
}
@Override
public void scrollTo(int x, int y) {
if(autoScrolling) return;
super.scrollTo(x, y);
}
答案 3 :(得分:0)
添加您的子布局
android:layout_gravity="right"
这应该有效(对我而言) 希望它有所帮助