【问题标题】:Add spacing between buttons in LinearLayout that's inside a HorizontalScrollView在 Horizo​​ntalScrollView 内的 LinearLayout 中的按钮之间添加间距
【发布时间】:2016-04-20 18:33:24
【问题描述】:

我尝试更改按钮的 X 位置,它可以工作,但按钮最终会被 Horizo​​ntalScrollView 卡住。我要做的就是在按钮之间添加间距。

这是我用来以编程方式显示按钮的代码:

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

这里是 Horizo​​ntalScrollView:

<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>

这是按钮当前的外观图片:

Picture

【问题讨论】:

    标签: android android-linearlayout horizontalscrollview spacing


    【解决方案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);
    

    这应该可以工作

    【讨论】:

    • 我尝试使用ActionBar.LayoutParams params = new ActionBar.LayoutParams(150, 150); params.setMargins(10, 0 ,0 ,10); button.setLayoutParams(params);,但也没有用
    • 您是否尝试过将 ActionBar.LayoutParams 更改为 LinearLayout.LayoutParams ?不过应该没关系。
    • 正确的用法是 LinearLayout.LayoutParams,是的。谢谢
    • 没问题。我已经编辑了答案。您会考虑将其标记为正确吗? (:
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-06
    • 2015-09-20
    • 2017-06-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多