簡體   English   中英

有沒有一種方法可以將ImageButton設置為始終是父級屏幕高度的1/4和父級屏幕寬度的1/3?

[英]Is there a way to set an ImageButton to always be 1/4 of the parent's screen height and 1/3 of the parent screen width?

我試圖在我的應用程序底部有3個ImageButtons(屏幕高度的1/4和屏幕寬度的1/3),並且我希望imageButtons能夠根據設備調整大小,而無需對大小進行硬編碼。 我嘗試將三個ImageButton放在線性布局中,並為所有三個ImageLayout使用weight_weight = 1,但是Images並沒有實際縮小,它只是裁剪了一部分圖像以適合其中的所有三個ImageButton。 非常感謝!

是的,您可以先找到屏幕的寬度和高度,如下所示:

    int width = getApplicationContext().getResources().getDisplayMetrics().widthPixels;
 int height = getApplicationContext().getResources().getDisplayMetrics().heightPixels; 

現在使用本教程轉換dp中的像素

然后按以下方式動態設置按鈕的高度和寬度:

 ViewGroup.LayoutParams params = myButton.getLayoutParams();
//Button new width
params.width = 400;

myButton.setLayoutParams(params);

嘗試這個

<?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:background="#FFFFFF"
android:orientation="vertical" >

<LinearLayout
    android:id="@+id/RLMain"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#CCFFFFFF"
    android:orientation="vertical"
    android:weightSum="4" >

    <LinearLayout
        android:id="@+id/LL1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="3"
        android:orientation="horizontal" >

        <!-- //...Your Other Layout Views here -->

    </LinearLayout>

    <LinearLayout
        android:id="@+id/LL2"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal"
        android:weightSum="9" >

        <ImageButton
            android:id="@+id/imageButton1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:layout_weight="1"
            android:src="#FF0000" />

        <ImageButton
            android:id="@+id/imageButton2"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:layout_weight="1"
            android:src="#FF0000" />

        <ImageButton
            android:id="@+id/imageButton3"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:layout_weight="1"
            android:src="#FF0000" />
    </LinearLayout>
</LinearLayout>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM