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>

MyClass.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 试图强制在我试图手动排列的视图上定位)。

4

2 回答 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>

于 2013-07-31T15:02:41.507 回答
0

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

于 2013-08-08T23:09:40.823 回答