繁体   English   中英

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

[英]How to correctly show Buttons in a LinearLayout inside a HorizontalScrollView?

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

这是使用API​​ 23(1080x1920 xxhdpi)的仿真器的结果:

带有API 23 1080x1920 xxhdpi的仿真器

这是使用API​​ 22在我的Android手机中的外观:

这是我在带有Api 22的Android手机中的外观:

这是我的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);
     }
}

主要活动:

public class MainActivity extends AppCompatActivity {

    LinearLayout viewCategoryNames;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        viewCategoryNames = (LinearLayout) findViewById(R.id.viewCategoryNames);
        buildCategoryScroll();

    }

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

        for (int i = 1; i <= 15; i++) {
            final Button btCategory = new Button(MainActivity.this);
            btCategory.setText(String.valueOf(i));
            btCategory.setTextSize(16f);
            btCategory.setAllCaps(false);
            btCategory.setBackgroundColor(ContextCompat.getColor(this, R.color.colorAccent));
            btCategory.setTextColor(ContextCompat.getColor(this, android.R.color.black));
            btCategory.setLayoutParams(layoutParams);
            btCategory.setTag(i);
            viewCategoryNames.addView(btCategory);
        }
    }
}

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/hsvClosetFilter"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_below="@+id/rlt"
    android:layout_marginTop="5dp"
    tools:context="com.example.rohantaneja.horizontalscrollview.MainActivity">

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

</HorizontalScrollView>

输出量

尝试这个

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM