web-dev-qa-db-fra.com

Android: Comment définir par programme la taille d'une mise en page

Dans le cadre d'une Android App, je construis un jeu de boutons. Les boutons font partie d'un ensemble imbriqué de LinearLayouts. À l'aide de poids, l'ensemble se redimensionne automatiquement en fonction de la taille du parent parent LinearLayout. 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 le jeu de boutons se redimensionne en fonction de ce changement.

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

J'ai essayé plusieurs techniques suggérées et aucune n'approche de travailler. Voici un sous-ensemble du XML qui construit le jeu de boutons:

    <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 accéder à numberPadLayout à partir de Java. Et une fois que j'ai accès à la vue, 2) comment modifier la hauteur et la largeur de la mise en page.

Toute suggestion sera appréciée.

129
codingCat

Java

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 en pixels, utilisez ceci:

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

Kotlin

352
A. Abiri

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

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();
}
2
smartmouse
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);
2
Kapil Parmar