如何在Horizo​​ntalScrollView内的LinearLayout中正确显示按钮?

时间:2017-03-16 22:53:04

标签: android layout uibutton android-linearlayout

我添加了一个带有LinearLayout的Horizo​​ntalScrollView,以编程方式添加按钮以显示类别选择器。

这是使用API​​ 23,1080x1920 xxhdpi:

的模拟器中的结果

Emulator with API 23 1080x1920 xxhdpi

这就是我在Android手机中使用API​​ 22的方式:

This is how it looks in my Android phone with Api 22:

这是我的xml代码:

<HorizontalScrollView
        android:id="@+id/hsvClosetFilter"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_below="@+id/rlt"
        android:layout_marginTop="5dp">

        <LinearLayout
            android:id="@+id/viewCategoryNames"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="horizontal" />

    </HorizontalScrollView>

我正在以编程方式添加按钮:

private void buildCategoryScroll() {
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(230, 80);
        layoutParams.setMargins(0, 10, 30, 10);

        for (int i=0; i<categoryNames.size(); i++) {
            final Button btCategory = new Button(getActivity());
            btCategory.setText(categoryNames.get(i));
            btCategory.setTextSize(16f);
            btCategory.setAllCaps(false);
            btCategory.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.colorPrimary));
            btCategory.setTextColor(ContextCompat.getColor(getActivity(), R.color.white));
            btCategory.setLayoutParams(layoutParams);
            btCategory.setTag(i);
            viewCategoryNames.addView(btCategory);
     }
}

2 个答案:

答案 0 :(得分:2)

MainActivity:

<div id="nav-main" class="nav-collapse collapse">
     <ul class="nav">
         <li>
             <a href='#somewhere'>Somewhere</a>
         </li>
     </ul>
</div>

activity_main.xml中:

$('.nav-collapse .nav a').on('click', function(){
    if ( $( '.nav-collapse' ).hasClass('in') ) {
        $('.navbar-toggle').click();
    }
});

enter image description here

答案 1 :(得分:0)

试试这个

第1步:

<LinearLayout android:id="@+id/viewCategoryNames"
  android:layout_width="match_parent" 
  android:layout_height="match_parent" 
  android:orientation="horizontal" /> 

第2步:

private void buildCategoryScroll() {
for (int i=0; i<categoryNames.size(); i++) { 
final Button btCategory = new Button(getActivity());
btCategory.setText(categoryNames.get(i)); 
btCategory.setTextSize(16f); 
btCategory.setAllCaps(false);
btCategory.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.colorPrimary)); 
btCategory.setTextColor(ContextCompat.getColor(getActivity(), R.color.white));
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(230, 80); 
layoutParams.setMargins(0, 10, 30, 10);
btCategory.setLayoutParams(layoutParams); 
btCategory.setTag(i); 
viewCategoryNames.addView(btCategory); 
} 
}