gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 17:01:14 24 4
gpt4 key购买 nike

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

这是在具有 API 23、1080x1920 xxhdpi 的模拟器中的结果:

Emulator with API 23 1080x1920 xxhdpi

这就是我使用 API 22 的 Android 手机的样子:

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

最佳答案

主要 Activity :

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>

Output

关于android - 如何在 Horizo​​ntalScrollView 内的 LinearLayout 中正确显示按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42846213/

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