Android:在选择项目后将其定位在屏幕的中心位置,在水平滚动视图中 [英] Android : Locating the item at center position of the screen after selecting it ,In the horizontalscrollview

查看:122
本文介绍了Android:在选择项目后将其定位在屏幕的中心位置,在水平滚动视图中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to scroll item to the middle position of the screen after selecting it.I have used Horizontalscrollview and LinearLayout inside it to add item. I show in the 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的中间?

推荐答案

经过长时间的尝试,我已经有了它

After long try , I have got it with this

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);
}

这篇关于Android:在选择项目后将其定位在屏幕的中心位置,在水平滚动视图中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆