【问题标题】:How to add views from right-to-left to HorizontalScrollView?如何将视图从右到左添加到 Horizo​​ntalScrollView?
【发布时间】:2014-12-08 10:49:18
【问题描述】:

我有一个带有 LinearLayout 的水平滚动视图,我在线性布局中添加了一些视图,这些视图是按钮!但问题是它们从左到右添加到线性布局,我希望它们从右到左添加,我该如何实现?

这是我的布局:

<HorizontalScrollView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/filteringSearchBox"
    android:layout_marginBottom="5dp"
    android:layout_marginTop="5dp"
    android:layoutDirection="rtl" >

    <LinearLayout
        android:id="@+id/filteringHeaderLL"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="10dp"
        android:gravity="end"
        android:layoutDirection="rtl"
        android:orientation="horizontal" >
    </LinearLayout>

</HorizontalScrollView>

我玩过 layoutDirection 和重力,它们都不起作用 请给我完整的答案

这是代码:

    for (int i = 0; i < mFilteringWB.length; i++) {
        Button mButton = new Button(mContext);
        mButton.setId(i);
        mButton.setTextColor(Color.parseColor("#6D6D6D"));
        mButton.setTextSize(13);
        mButton.setBackgroundResource(R.drawable.shad_filtering);
        mButton.setTypeface(mFace);
        mButton.setText(mFilteringWB[i]);
        mLayout.addView(mButton);
    }

【问题讨论】:

  • 添加将这些按钮添加到滚动视图的代码。
  • @hasan83 我添加了代码,编辑了我的问题,我想在最右侧线性输出按钮 0,这意味着我想从右到左开始布局顺序
  • 添加新视图后,您必须再次滚动到所需位置。

标签: android right-to-left horizontalscrollview


【解决方案1】:

使用addView(View child, int index) 代替void addView(View child)

mylinearLayout.add(myButton, 0); // add new views to index 0 which is left

【讨论】:

  • 应用程序在此崩溃,说长度为 6 并且 count=0 !
  • @MahdiGiveie 你在哪一行得到了那个错误?我认为这与另一件事有关
  • @hasan83 这一行:mLayout.addView(mButton,1);
  • @MahdiGiveie 为什么是 1?它必须是0,你想在第一个位置“0”静态有一些视图吗?
  • mLayout.addView(mButton,0);不是 mLayout.addView(mButton,1);请重新测试。
猜你喜欢
  • 2015-05-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多