Android 如何在水平滚动视图中正确显示线性布局中的按钮?
我添加了一个带有LinearLayout的HorizontalScrollView,按钮是以编程方式添加的,以显示类别选择器 这是使用API 23 1080x1920 xxhdpi的模拟器中的结果: 这是我的Android手机在API 22中的外观: 这是我的xml代码:Android 如何在水平滚动视图中正确显示线性布局中的按钮?,android,layout,uibutton,android-linearlayout,Android,Layout,Uibutton,Android Linearlayout,我添加了一个带有LinearLayout的HorizontalScrollView,按钮是以编程方式添加的,以显示类别选择器 这是使用API 23 1080x1920 xxhdpi的模拟器中的结果: 这是我的Android手机在API 22中的外观: 这是我的xml代码: <HorizontalScrollView android:id="@+id/hsvClosetFilter" android:layout_width="match_parent"
<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);
}
}
private void buildCategoryScroll(){
LinearLayout.LayoutParams LayoutParams=新的LinearLayout.LayoutParams(230,80);
layoutParams.setMargins(0,10,30,10);
对于(inti=0;i试试这个
步骤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);
}
}
private void buildCategoryScroll(){
对于(inti=0;i试试这个
步骤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);
}
}
private void buildCategoryScroll(){
对于(int i=0;imain)活动:
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);
}
}
}
public类MainActivity扩展了AppCompatActivity{
线性布局视图类别名称;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewCategoryNames=(LinearLayout)findViewById(R.id.viewCategoryNames);
buildCategoryScroll();
}
私有void buildCategoryScroll(){
LinearLayout.LayoutParams LayoutParams=新的LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_内容,ViewGroup.LayoutParams.WRAP_内容);
layoutParams.setMargins(0,10,30,10);
对于(inti=1;imain)活动:
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);
}
}
}
public类MainActivity扩展了AppCompatActivity{
线性布局视图类别名称;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewCategoryNames=(LinearLayout)findViewById(R.id.viewCategoryNames);
buildCategoryScroll();
}
私有void buildCategoryScroll(){
LinearLayout.LayoutParams LayoutParams=新的LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_内容,ViewGroup.LayoutParams.WRAP_内容);
layoutParams.setMargins(0,10,30,10);
对于(int i=1;我感谢您的回答,但它不起作用..我在emulator中得到了与答案相同的结果,但它不起作用..我在emulator中得到了相同的结果