2011-07-23 3 views
102

Dans le cadre d'une application Android, je construis un ensemble de boutons. Les boutons font partie d'un ensemble imbriqué de LinearLayouts. En utilisant le poids, j'ai l'ensemble se redimensionner automatiquement en fonction de la taille du parent LinearLayout contenant. L'idée est, en fonction du nombre de pixels et de la densité de l'écran, de définir la taille de la disposition contenant à un nombre de pixels; et que l'ensemble de boutons se redimensionne en fonction de ce changement.Android: Comment programmer la taille d'une mise en page

La question est alors: Comment redimensionner la mise en page.

J'ai essayé plusieurs techniques suggérées, et aucune approche n'est proche de mon travail. Voici un sous-ensemble de XML qui construit l'ensemble de bouton:

<LinearLayout android:layout_height="104pt" android:id="@+id/numberPadLayout" android:orientation="horizontal" android:layout_width="104pt" 
    android:background="#a0a0a0" 
    android:layout_alignParentBottom="true" 
    android:layout_centerHorizontal="true" 
> 

    <LinearLayout android:layout_weight="2" android:layout_height="fill_parent" android:id="@+id/linearLayout1" android:orientation="vertical" android:layout_width="wrap_content"> 
     <Button android:text="1" android:id="@+id/button1" android:layout_weight="2" android:layout_width="fill_parent" android:layout_height="wrap_content"></Button> 
     <Button android:text="4" android:id="@+id/button4" android:layout_weight="2" android:layout_width="fill_parent" android:layout_height="wrap_content"></Button> 
     <Button android:text="7" android:id="@+id/button7" android:layout_weight="2" android:layout_width="fill_parent" android:layout_height="wrap_content"></Button> 
     <Button android:text="-" android:id="@+id/buttonDash" android:layout_weight="2" android:layout_width="fill_parent" android:layout_height="wrap_content"></Button> 
    </LinearLayout> 
    <LinearLayout android:layout_weight="2" android:layout_height="fill_parent" android:id="@+id/linearLayout2" android:orientation="vertical" android:layout_width="wrap_content"> 
     <Button android:text="2" android:id="@+id/button2" android:layout_weight="2" android:layout_width="fill_parent" android:layout_height="wrap_content"></Button> 
     <Button android:text="5" android:id="@+id/button5" android:layout_weight="2" android:layout_width="fill_parent" android:layout_height="wrap_content"></Button> 
     <Button android:text="8" android:id="@+id/button8" android:layout_weight="2" android:layout_width="fill_parent" android:layout_height="wrap_content"></Button> 
     <Button android:text="0" android:id="@+id/button0" android:layout_weight="2" android:layout_width="fill_parent" android:layout_height="wrap_content"></Button> 
    </LinearLayout> 

    <LinearLayout android:layout_weight="2" android:layout_height="fill_parent" android:id="@+id/linearLayout3" android:orientation="vertical" android:layout_width="wrap_content"> 
     <Button android:text="3" android:id="@+id/button3" android:layout_weight="2" android:layout_width="fill_parent" android:layout_height="wrap_content"></Button> 
     <Button android:text="6" android:id="@+id/button6" android:layout_weight="2" android:layout_width="fill_parent" android:layout_height="wrap_content"></Button> 
     <Button android:text="9" android:id="@+id/button9" android:layout_weight="2" android:layout_width="fill_parent" android:layout_height="wrap_content"></Button> 
     <Button android:text="." android:id="@+id/buttonDot" android:layout_weight="2" android:layout_width="fill_parent" android:layout_height="wrap_content"></Button> 
    </LinearLayout> 
    <LinearLayout android:layout_weight="2" android:layout_height="fill_parent" android:id="@+id/linearLayout4" android:orientation="vertical" android:layout_width="wrap_content"> 
     <Button android:text="/" android:id="@+id/buttonBack" android:layout_weight="2" android:layout_width="fill_parent" android:layout_height="wrap_content"></Button> 
     <Button android:text="\" android:id="@+id/buttonEnter" android:layout_weight="2" android:layout_width="fill_parent" android:layout_height="wrap_content"></Button> 
    </LinearLayout> 

</LinearLayout>  

Les deux questions sont les suivantes: 1) Comment puis-je avoir accès à numberPadLayout de Java. Et une fois que j'ai accès à la vue, 2) comment puis-je changer la hauteur et la largeur de la mise en page.

Toutes les suggestions seront appréciées.

Répondre

294

Cela devrait fonctionner:

// Gets linearlayout 
LinearLayout layout = findViewById(R.id.numberPadLayout); 
// Gets the layout params that will allow you to resize the layout 
LayoutParams params = layout.getLayoutParams(); 
// Changes the height and width to the specified *pixels* 
params.height = 100; 
params.width = 100; 
layout.setLayoutParams(params); 

Si vous voulez convertir dip aux pixels, utilisez ceci:

int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, <HEIGHT>, getResources().getDisplayMetrics()); 
+8

Merci, c'était exactement ce dont j'avais besoin. Je semble avoir mal tourné avec mon hypothèse que je devrais appliquer les paramètres à la mise en page pour son travail. – codingCat

+1

Combinez ceci avec http://stackoverflow.com/a/8171014/9648 si vous avez besoin de changer en fonction des valeurs calculées d'autres vues. – JohnnyLambada

+1

Cela ne fonctionne pas. Ma largeur de mise en page est de 2000. Lorsque j'imprime 'layout.getWidth()', c'est toujours 2000 même si je mets 'params.width = 1000' –

44

mon exemple de code

wv = (WebView) findViewById(R.id.mywebview); 
wv.getLayoutParams().height = LayoutParams.MATCH_PARENT; // LayoutParams: android.view.ViewGroup.LayoutParams 
// wv.getLayoutParams().height = LayoutParams.WRAP_CONTENT; 
wv.requestLayout();//It is necesary to refresh the screen 
+0

WebView utilisation sent comme vous l'utilisez dans les applications cordova, non? –

2

Vous pouvez obtenir la hauteur réelle de la mise en page appelée avec ce code:

public int getLayoutSize() { 
// Get the layout id 
    final LinearLayout root = (LinearLayout) findViewById(R.id.mainroot); 
    final AtomicInteger layoutHeight = new AtomicInteger(); 

    root.post(new Runnable() { 
    public void run() { 
     Rect rect = new Rect(); 
     Window win = getWindow(); // Get the Window 
       win.getDecorView().getWindowVisibleDisplayFrame(rect); 

       // Get the height of Status Bar 
       int statusBarHeight = rect.top; 

       // Get the height occupied by the decoration contents 
       int contentViewTop = win.findViewById(Window.ID_ANDROID_CONTENT).getTop(); 

       // Calculate titleBarHeight by deducting statusBarHeight from contentViewTop 
       int titleBarHeight = contentViewTop - statusBarHeight; 
       Log.i("MY", "titleHeight = " + titleBarHeight + " statusHeight = " + statusBarHeight + " contentViewTop = " + contentViewTop); 

       // By now we got the height of titleBar & statusBar 
       // Now lets get the screen size 
       DisplayMetrics metrics = new DisplayMetrics(); 
       getWindowManager().getDefaultDisplay().getMetrics(metrics); 
       int screenHeight = metrics.heightPixels; 
       int screenWidth = metrics.widthPixels; 
       Log.i("MY", "Actual Screen Height = " + screenHeight + " Width = " + screenWidth); 

       // Now calculate the height that our layout can be set 
       // If you know that your application doesn't have statusBar added, then don't add here also. Same applies to application bar also 
       layoutHeight.set(screenHeight - (titleBarHeight + statusBarHeight)); 
       Log.i("MY", "Layout Height = " + layoutHeight); 

      // Lastly, set the height of the layout  
      FrameLayout.LayoutParams rootParams = (FrameLayout.LayoutParams)root.getLayoutParams(); 
      rootParams.height = layoutHeight.get(); 
      root.setLayoutParams(rootParams); 
     } 
    }); 

return layoutHeight.get(); 
} 
0
LinearLayout YOUR_LinearLayout =(LinearLayout)findViewById(R.id.YOUR_LinearLayout) 
    LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
         /*width*/ ViewGroup.LayoutParams.MATCH_PARENT, 
       /*height*/ 100, 
       /*weight*/ 1.0f 
       ); 
       YOUR_LinearLayout.setLayoutParams(param); 
Questions connexes