태그 보관물: layout

layout

Android : 프로그래밍 방식으로 레이아웃 크기를 설정하는 방법 XML의 서브 세트입니다. <LinearLayout

안드로이드 앱의 일부로 버튼 세트를 만들고 있습니다. 버튼은 중첩 된 LinearLayouts 세트의 일부입니다. 가중치를 사용하여 포함하는 부모 LinearLayout의 크기에 따라 자동으로 크기 조정 세트가 설정되었습니다. 아이디어는 화면의 픽셀 수와 밀도에 따라 포함 레이아웃의 크기를 여러 픽셀로 설정하는 것입니다. 그 변화에 따라 버튼 크기를 조정하십시오.

문제는 레이아웃의 크기를 조정하는 방법입니다.

나는 몇 가지 제안 된 기술을 시도했지만 아무도 작동하지 않습니다. 다음은 단추 세트를 작성하는 XML의 서브 세트입니다.

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

두 가지 질문은 다음과 같습니다. 1) Java에서 numberPadLayout에 액세스하는 방법 뷰에 액세스하면 2) 레이아웃의 높이와 너비를 어떻게 변경합니까?

모든 제안을 부탁드립니다.



답변

자바

이것은 작동해야합니다 :

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

딥을 픽셀로 변환하려면 다음을 사용하십시오.

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

코 틀린


답변

내 샘플 코드

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

답변

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

답변

이 코드를 사용하여 호출 된 레이아웃의 실제 높이를 얻을 수 있습니다.

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

답변