2016-05-08 32 views
0

Ich möchte zwei lineare Layouts nach einer Layouts zu einer Aktivität programmatisch jeweils der gleichen Breite hinzufügen. Das Problem ist, dass ich die Gewichte dieser Layouts nicht programmatisch einstellen kann. Ich könnte dies innerhalb von XML tun, aber ich möchte dies im Programm tun. hier ist das, was ich will:Geordnetes lineares Android-Layout für benutzerdefiniertes Design

all are button

erste Schaltfläche enthält volle Breite des Bildschirms aber nach dieser beide Taste gleiche Breite teilen und so weiter ...

offensichtlich, muß es programmatisch tun.

mein Layout:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:background="@drawable/background" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    xmlns:tools="http://schemas.android.com/tools" 
    tools:context=".HomeActivity"> 

    <ScrollView 
     android:id="@+id/scrollView1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

     <LinearLayout 
      android:id="@+id/linearLayout1" 
      android:orientation="vertical" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 
     </LinearLayout> 


    </ScrollView> 
</LinearLayout> 

Code behinds:

Button button = new Button(HomeActivity.this); 
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, 564); 
         layoutParams.setMargins(60, 80, 60, 40); 

Drawable drawable = Drawable.createFromStream(getAssets().open("image/GridButton/NovoTel_IGW_lg.png"), null); 
button.setBackground(drawable); 
button.setLayoutParams(layoutParams); 

linearLayout.addView(button); 

Ich habe Ausgabe wie folgt:

enter image description here

Bitte legen nahe, was der beste Weg ist.

Antwort

1

Hier ist die Antwort. Ich habe es probiert. Wenn es erfüllt dann als Antwort markiert:

try { 

    DisplayMetrics dm = new DisplayMetrics(); 
    this.getWindow().getWindowManager().getDefaultDisplay().getMetrics(dm); 
    int width = dm.widthPixels; 
    int j = 0 ; 
    String image; 
    for(int i=0;i<(**JsonArray**).length();i++) { 

     JSONObject jsonObject1 = **JsonArray**.getJSONObject(i); 


     if(i==0) { 
      image = //DrawableImage; 
      drawable = Drawable.createFromStream(getAssets().open(image), null); 
      Button button = new Button(HomeActivity.this); 
      LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 564); 
      layoutParams.setMargins(60, 80, 60, 40); 
      button.setBackground(drawable); 
      button.setLayoutParams(layoutParams); 
      linearLayout.addView(button); 
     } 
     else 
     { 
      if(i%2 == 1) 
      { 
       image = //DrawableImage; 
       drawable = Drawable.createFromStream(getAssets().open(image), null); 
       LinearLayout linearLayout1=new LinearLayout(this); 
       linearLayout1.setOrientation(LinearLayout.HORIZONTAL); 
       Button button = new Button(HomeActivity.this); 
       LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(width/2-120, 564); 
       layoutParams.setMargins(60, 80, 60, 40); 
       button.setBackground(drawable); 
       button.setLayoutParams(layoutParams); 
       linearLayout1.addView(button); 


       if(i+1 < data.length()) 
       { 
        JSONObject jsonObject2 = data.getJSONObject(i+1); 
        image = //DrawableImage; 
        drawable = Drawable.createFromStream(getAssets().open(image), null); 
        Button button2 = new Button(HomeActivity.this); 
        button2.setBackground(drawable); 
        button2.setLayoutParams(layoutParams); 
        linearLayout1.addView(button2); 

       } 

       linearLayout.addView(linearLayout1); 

      } 
      else 
      { 
       continue; 
      } 

     } 


    } 




}catch (IOException ex) 
{ 
    return; 
} 
+0

Vielen Dank. Es erfüllt meine Anfrage. –

2

Der einfachste Weg, um dies zu erreichen, wäre die Verwendung von LinearLayout (s) mit Gewicht. Hier ist ein einfaches Beispiel:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:orientation="vertical" 
android:weightSum="3"> 


    <Button android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"/> 
    <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:orientation="horizontal" 
      android:layout_weight="1" 
      android:weightSum="2"> 
     <Button android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"/> 
     <Button android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"/> 
    </LinearLayout> 
    <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:orientation="horizontal" 
      android:layout_weight="1" 
      android:weightSum="2"> 
     <Button android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"/> 
     <Button android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"/> 
    </LinearLayout> 
</LinearLayout> 

Ausgang: enter image description here

Dann können Sie beginnen, Ihre Grafiken zu implementieren. Sie benötigen keine ScrollView in dem vorgeschlagenen Layout, aber natürlich können Sie es später hinzufügen, wenn es notwendig wird.

+1

Vielen Dank, aber ich brauche es programmgesteuert. das ist das Problem. Wenn ein Element dann wie oben aussieht, aber mehr als eins, dann wird ein kleiner Teil hinzugefügt und so weiter. –

+0

Haben Sie versucht mit 'layoutParam.weight = 1;'? –

+0

Ich habe versucht aber keine Veränderung. Bitte geben Sie mir den Detailcode. –

Verwandte Themen