2017-03-16 6 views
0

Ich habe eine HorizontalScrollView mit einem LinearLayout hinzugefügt, Schaltflächen werden programmgesteuert hinzugefügt, um einen Kategorien-Selektor anzuzeigen. Wie werden Schaltflächen in einem LinearLayout in einem HorizontalScrollView korrekt angezeigt?

Dies ist das Ergebnis in einem Emulator mit API 23, 1080x1920 xxhdpi:

Emulator with API 23 1080x1920 xxhdpi

Dies ist, wie es mit API 22 in meinem Android-Handy aussieht:

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

Diese ist mein XML-Code:

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

Und ich bin Hinzufügen von Schaltflächen programmatisch wie folgt aus:

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

Antwort

1

MainActivity:

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

+0

Dank! Diese Arbeit am Emulator und meinem Android! –

0

dieses Versuchen

Schritt 1:

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

Schritt 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); 
} 
} 
+0

Danke für die Antwort, aber es hat nicht funktioniert .. Ich habe das gleiche Ergebnis in Emulator –

Verwandte Themen