【问题标题】: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?有没有办法将 ImageButton 设置为始终为父屏幕高度的 1/4 和父屏幕宽度的 1/3?
【发布时间】:2015-11-28 04:24:39
【问题描述】:

我试图在我的应用程序底部放置 3 个 ImageButtons(屏幕高度的 1/4 和屏幕宽度的 1/3),并且我希望 imageButtons 能够根据设备调整大小,而无需硬编码大小。 我尝试将三个 ImageButtons 放在一个线性布局中,并对它们三个都使用 layout_weight=1,但是图像实际上并没有按比例缩小,它只是裁剪了图像的一部分以适合所有三个 ImageButtons。 非常感谢!

【问题讨论】:

  • 请发布您的代码!!!这将有助于回答@Jay Xu

标签: java android android-layout imageview imagebutton


【解决方案1】:

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

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

现在使用 tutorial 将像素转换为 dp

然后将按钮的高度和宽度动态设置为-

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

myButton.setLayoutParams(params);

【讨论】:

  • 谢谢!我试过了,但是 Activity 会意外停止...我在 mainActivity 的 setContentView 之前执行了那些,onCreate() 方法...
【解决方案2】:

试试这个

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

【讨论】:

  • 谢谢你!我应该先用不同的 LinearLayout 分隔不同的区域来设计布局,然后在其中添加项目。
  • 您只需根据需要修改线性布局的 Weightsum 和 Weight。
猜你喜欢
  • 2014-02-09
  • 1970-01-01
  • 2017-02-01
  • 2016-05-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-01
相关资源
最近更新 更多