【问题标题】:Android : Locating the item at center position of the screen after selecting it ,In the horizontalscrollviewAndroid:选中项目后,将项目定位在屏幕的中心位置,在水平滚动视图中
【发布时间】:2014-05-23 09:54:06
【问题描述】:

我试图在选择项目后将项目滚动到屏幕的中间位置。我在其中使用了 Horizo​​ntalscrollview 和 LinearLayout 来添加项目。我在 XML 中显示

XML

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

  <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/bottom"
        android:orientation="vertical" >

       <HorizontalScrollView
                android:id="@+id/scrollView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/updown"
                android:layout_weight="0.8"
                android:fadingEdgeLength="0dp" >

                <LinearLayout
                    android:id="@+id/horizontalbar"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@color/tlbackground"
                    android:baselineAligned="true"
                    android:orientation="horizontal" >
                </LinearLayout>
       </HorizontalScrollView>
  </LinearLayout>
</RelativeLayout>

public class MainActivity extends FragmentActivity implements ActionBar.TabListener
{

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      hsl = (HorizontalScrollView) findViewById(R.id.scrollView);
      LinearLayout l1 = (LinearLayout)findViewById(R.id.horizontalbar);

        for(int i = 0; i < 20; i++)
        {
            Button b = new Button(this);
            b.setText("t"+i);
            ll.addView(b);
        }
     }
}

我想要如图所示的东西

点击前

点击后

如何在 Horizo​​ntalScrollview 中将所选项目滚动到中间?

【问题讨论】:

    标签: android scroll center horizontalscrollview


    【解决方案1】:

    经过长时间的尝试,我得到了它

    for (int i = 0; i < 20; i++) {
        final Button b = new Button(this);
        b.setText("t" + i);
        b.setOnClickListener(new OnClickListener() {
    
        @Override
        public void onClick(View v) {
           int scrollX = (b.getLeft() - (screenWidth / 2)) + (b.getWidth() / 2);
           hsl.smoothScrollTo(scrollX, 0);
        }
    });
    
    ll.addView(b);
    }
    

    【讨论】:

    • width ?是某种属性吗?它代表什么?
    • width 是整个设备的屏幕宽度,您可以从这里获得它stackoverflow.com/questions/21454511/…
    • 谢谢!您可以更改为screenWidth。我想这会更清楚。
    • 我已经为我的水平滚动视图设置了左右填充,点击的位置没有来到中心,int padding = getResources().getDisplayMetrics().widthPixels / 2; Horizo​​ntalScrollView1.setPadding(padding, 0, padding, 0); Horizo​​ntalScrollView1.setClipToPadding(false);
    【解决方案2】:

    您的 onCreate 应该是这样的:

    hsl = (HorizontalScrollView) findViewById(R.id.scrollView);
    LinearLayout ll = (LinearLayout) findViewById(R.id.horizontalbar);
    
    Display display = getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    final int width = size.x;
    
    for (int i = 0; i < 20; i++) {
        final Button b = new Button(this);
        b.setText("t" + i);
        b.setOnClickListener(new OnClickListener() {
    
            @Override
            public void onClick(View v) {
                int center = (width - b.getWidth())/2;
                hsl.scrollTo(b.getLeft() - center, b.getTop());
            }
        });
    
        ll.addView(b);
    }
    

    我唯一没有发现的是为什么我需要减去额外的 200 像素(在我的情况下)。可能是一些填充或其他东西。而不是 200,您可以发现它是多少次下降并调用函数:

    public static int convertDipToPixels(Context c, float dips) {
        return (int) (dips * c.getResources().getDisplayMetrics().density + 0.5f);
    }
    

    【讨论】:

    • 试试 b.getWidth() 而不是 b.getLayoutParams().width
    • 感谢@Zoran 的回复,但经过一番尝试,我得到了它。
    • @Zoran,你在哪里减去额外的 200 像素?
    • 我已经为我的水平滚动视图设置了左右填充,int padding = getResources().getDisplayMetrics().widthPixels / 2; Horizo​​ntalScrollView1.setPadding(padding, 0, padding, 0); Horizo​​ntalScrollView1.setClipToPadding(false);
    猜你喜欢
    • 2019-03-04
    • 1970-01-01
    • 2010-10-08
    • 1970-01-01
    • 2010-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-01
    相关资源
    最近更新 更多