为什么我不能强制FrameLayout的高度?

问题描述 投票:2回答:2

我将FrameLayout的高度设置为400,但它仍为64。

frame_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
</FrameLayout>

my class.Java

container = (FrameLayout) inflater.inflate(R.layout.frame_layout, scrollFrame, false);
scrollFrame.addView(container);
container.setLayoutParams(new 
    ScrollView.LayoutParams(ScrollView.LayoutParams.MATCH_PARENT, 400));
Log.d("Output", "FrameLayout Height: " + container.getHeight());

产量

FrameLayout Height: 64

即使我在xml中设置layout_height =“400”,它仍然是64.我认为这与ScrollViews有关。 (我不能使用LinearLayouts,因为它们会给我的代码带来一些其他问题。)我希望FrameLayout根据我放在其中的视图动态调整大小。 (一个LinearLayout自动调整大小,但它调整到我试图显示的高度的10倍,使得ScrollView太大;然后有一个定位问题,因为LinearLayout试图强制我试图手动安排的视图上的位置)。

android android-layout resize scrollview android-framelayout
2个回答
0
投票

尝试使用weightSums和布局权重。这是一个有两个框架的布局,它总是占据屏幕的50%,无论放入它们的内部和高度。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          tools:context="com.nimbits.android.HomeActivity"
          android:weightSum="1.0">




<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_alignParentLeft="true"
    android:layout_above="@+id/progressBar"
    android:weightSum="1.0"
    android:orientation="vertical">

    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:id="@id/main_frame"
        android:layout_weight=".5">


    </FrameLayout>

    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:id="@id/data_frame"
        android:layout_weight=".5"
        android:layout_alignParentEnd="false">


    </FrameLayout>
</LinearLayout>


0
投票

因此,虽然我无法准确回答为什么FrameLayout不能给出特定的大小,但我认为它与FrameLayout的设计有关。我试图做的最终是滥用UI和我的应用程序的糟糕的编程设计方法。因此,如果您认为需要手动调整FrameLayout的大小,请问自己是否真的有必要 - 可能有更好的方法。

© www.soinside.com 2019 - 2024. All rights reserved.