【问题标题】:Android view dynamic size [duplicate]Android视图动态大小[重复]
【发布时间】:2016-04-26 19:40:19
【问题描述】:

我有一个包含多个视图的布局,其中一个视图必须根据屏幕尺寸进行缩放。到目前为止,我已将其设置为 250dp,因为它在 5.1 英寸设备上看起来不错,但在较小的设备上使用时就不再那么好了。我不能使用换行内容,因为它会使它看起来很糟糕并且按钮的大小太小,所以它必须是屏幕宽度的 1/3。是否可以在布局代码中做到这一点,还是必须在 java 代码中做到这一点?

【问题讨论】:

  • 来自您提供的链接...视图的实际测量工作在onMeasure(int,int)中执行,由该方法调用。因此,只有 onMeasure(int, int) 可以而且必须被子类覆盖。

标签: java android


【解决方案1】:

是否可以在布局代码中做到这一点

是的。通过使用LinearLayoutandroid:layout_weightandroid:weightSum 属性非常简单:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
   android:layout_width="match_parent" 
   android:layout_height="match_parent" 
   android:weightSum="3"
   android:orientation="horizontal"> 

   <View 
      android:id="@+id/thirdView" 
      android:layout_height="wrap_content" 
      android:layout_width="0dp" 
      android:layout_weight="1" />

</LinearLayout>

【讨论】:

  • 我喜欢举重,但那里的负载很重......:D
【解决方案2】:

您可以查看Percent Support Library 中的PercentRelativeLayout。这是一个RelativeLayout,您可以在其中以百分比指定它的视图大小,例如将其宽度设置为 33%。

<android.support.percent.PercentRelativeLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:app="http://schemas.android.com/apk/res-auto"
     android:layout_width="match_parent"
     android:layout_height="match_parent">

     <Button
         app:layout_widthPercent="33%"
         app:layout_heightPercent="33%"
         app:layout_marginTopPercent="25%"
         app:layout_marginLeftPercent="25%" />

</android.support.percent.PercentRelativeLayout>

【讨论】:

  • 我不断收到一个异常,基本上是它在我的游戏包中找不到值
  • @TheRealPolarbear 确切的错误信息是什么?您是否正确包含库?
  • 我必须添加 .jar 吗?这解释了错误
  • 好吧,它不像我看到的那么容易。我正在使用 eclipse,所以我必须制作一个 build.gradle 文件并将其添加到任何文件中,以便像这样导入它还是我可以将它添加到 java 构建路径中?
  • @TheRealPolarbear 使用 Android Studio 会很容易。对于 Eclipse,您可以尝试包含位于 [android-sdk]/extras/android/compatibility/percent/ 的库
【解决方案3】:

您需要覆盖 onMeasure(...) 然后在此函数中调整视图大小登录。 Here 是完美的解释和解决方案。 此链接中的一些参考代码如下...

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

    int desiredWidth = 100;
    int desiredHeight = 100;

    int widthMode = MeasureSpec.getMode(widthMeasureSpec);
    int widthSize = MeasureSpec.getSize(widthMeasureSpec);
    int heightMode = MeasureSpec.getMode(heightMeasureSpec);
    int heightSize = MeasureSpec.getSize(heightMeasureSpec);

    int width;
    int height;

    //Measure Width
    if (widthMode == MeasureSpec.EXACTLY) {
        //Must be this size
        width = widthSize;
    } else if (widthMode == MeasureSpec.AT_MOST) {
        //Can't be bigger than...
        width = Math.min(desiredWidth, widthSize);
    } else {
        //Be whatever you want
        width = desiredWidth;
    }

    //Measure Height
    if (heightMode == MeasureSpec.EXACTLY) {
        //Must be this size
        height = heightSize;
    } else if (heightMode == MeasureSpec.AT_MOST) {
        //Can't be bigger than...
        height = Math.min(desiredHeight, heightSize);
    } else {
        //Be whatever you want
        height = desiredHeight;
    }

    //MUST CALL THIS
    setMeasuredDimension(width, height);
}

【讨论】:

  • 不是整个布局本身吗?我想调整单个视图而不是布局
  • 您的视图必须位于容器内?
  • 布局是在 .xml 文件中制作的。许多类型之一的布局文件布局中的基础(例如 FrameLayout、RelativeLayout 和 LinearLayout)。这算不算一个容器?
  • 那谁来加sn-ps代码呢?
  • 什么意思?我不明白提示 m8
猜你喜欢
  • 1970-01-01
  • 2012-08-20
  • 2013-12-18
  • 2012-04-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-08
相关资源
最近更新 更多