Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/400.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android视图动态大小_Java_Android - Fatal编程技术网

Java Android视图动态大小

Java Android视图动态大小,java,android,Java,Android,我有一个有几个视图的布局,其中一个视图必须根据屏幕大小进行缩放。到目前为止,我已经将其设置为250dp,因为它在5.1英寸的设备上看起来不错,但在较小的设备上使用时,它就不再那么好了。我不能使用包装内容,因为它会使它的外观非常糟糕,按钮的大小太小,所以它必须是屏幕宽度的1/3。是否可以在布局代码中执行此操作,还是必须在java代码中执行此操作?您需要覆盖onMeasure(…),然后在此函数中执行视图大小调整登录。是完美的解释和解决方案。 下面是这个链接中的一些参考代码 @Override pr

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

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

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

你可以从窗户上看一看。这是一个
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>

在布局代码中可以这样做吗

是的。使用
android:layou-weight
android:weightSum
LinearLayout
属性非常简单:

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


这不就是整个布局本身吗?我想重新zise单个视图而不是布局您的视图必须位于容器中?布局是在.xml文件中创建的。布局文件布局中多种类型之一的基础(例如FrameLayout、RelativeLayout和LinearLayout)。这算是一个容器吗?那么谁会添加代码片段?你是什么意思?我不明白你给我的链接中的提示。。。视图的实际测量工作在onMeasure(int,int)中执行,该方法调用onMeasure。因此,只有onMeasure(int,int)可以而且必须被子类覆盖。我一直得到一个例外,基本上是它在我的游戏中找不到值package@TheRealPolarbear确切的错误信息是什么?你是否正确地包含了库?我是否必须添加一个.jar?这就解释了这个错误好吧,这并不像我看到的那么容易。我在使用eclipse,所以我必须创建一个build.gradle文件并将其添加到任何东西中,以便像那样导入它,还是我可以将其添加到java构建路径中?@TheRealPolarbear使用Android Studio会很容易。。对于Eclipse,您可以尝试包含位于[android sdk]/extras/android/compatibility/percent/I like weights但负载沉重的库…:D