gpt4 book ai didi

Android : Locating the item at center position of the screen after selecting it , 在水平 ScrollView 中

转载 作者:太空宇宙 更新时间:2023-11-03 11:30:01 24 4
gpt4 key购买 nike

我试图在选择项目后将项目滚动到屏幕的中间位置。我在其中使用了 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);
}
}
}

我想要如图所示的东西

点击前

enter image description here

点击后

enter image description here

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

最佳答案

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

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 : Locating the item at center position of the screen after selecting it , 在水平 ScrollView 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23826115/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com