我有一个包含水平线性布局的HorizontalScrollView,当我添加新按钮到水平线性布局时,按钮会从左到右依次添加在一起,当超出屏幕宽度时,新添加的按钮将无法在屏幕上显示,但是会出现一个水平滚动条以向右滚动。
我想使添加新按钮到水平线性布局的方向从右到左而不是从左到右。
![enter image description here](https://istack.dev59.com/rDdS3.webp)
我想使添加新按钮到水平线性布局的方向从右到左而不是从左到右。
![enter image description here](https://istack.dev59.com/xUjnR.webp)
当你使用时:
myLinearLayout.addView(myButton);
它将它们一个接一个地添加。但是如果您使用
myLinearLayout.addView(myButton, 0);
这段代码在LinearLayout的第一个项目前添加了myButton
。
除了上面的代码,如果您希望您的HorizontalScrollView从右侧开始,可以使用以下代码:
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
hScrollView.fullScroll(HorizontalScrollView.FOCUS_RIGHT);
}
}, 100L);
<HorizontalScrollView
android:id="@+id/scrool"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:rotation="180"
android:fillViewport="true">
<LinearLayout
android:id="@+id/lytAll"
android:layout_width="match_parent"
android:rotation="180"
android:layout_height="match_parent"
android:gravity="right"
android:orientation="horizontal"/>
</HorizontalScrollView>
你可以通过使用相对布局并添加具有以下属性的新按钮来实现这一点:
android:layout_toLeftOf=""