我试图改变按钮的X位置,它可以工作,但按钮最后被HorizontalScrollView捕获。我想要做的就是在按钮之间添加间距。
以下是我用来以编程方式显示按钮的代码:
@TargetApi(21)
private void loadProfiles() {
HorizontalScrollView scrollView = (HorizontalScrollView) findViewById(R.id.horizontalScrollView);
LinearLayout layout = (LinearLayout) scrollView.findViewById(R.id.scrollLinear);
if(getFilesDir().listFiles().length != 0) {
for(File file : getFilesDir().listFiles()) {
ImageButton button = new ImageButton(this);
button.setLayoutParams(new ActionBar.LayoutParams(150, 150));
button.setY(35);
button.setBackground(getResources().getDrawable(R.drawable.roundedbutton, getTheme()));
layout.addView(button);
}
}
}
这是HorizontalScrollView:
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="100dp"
android:background="@drawable/bar"
android:id="@+id/horizontalScrollView"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" >
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/scrollLinear"></LinearLayout>
</HorizontalScrollView>
这是按钮当前的样子:
答案 0 :(得分:1)
ImageButton button = new ImageButton(this);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(150, 150);
layoutParams.setMargins(10,0,0,10);
button.setLayoutParams(layoutParams);
button.setY(35);
button.setBackground(getResources().getDrawable(R.drawable.roundedbutton, getTheme()));
layout.addView(button);
这应该有效