【问题标题】:Why can't I force the height of my FrameLayout?为什么我不能强制 FrameLayout 的高度?
【发布时间】:2012-10-10 05:44:11
【问题描述】:

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

【问题讨论】:

    标签: android android-layout resize scrollview android-framelayout


    【解决方案1】:

    尝试使用 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>
    

    【讨论】:

    • 好吧,我实际上是在尝试将框架宽度设置为特定大小,而不是加权比率。我最终只使用了一个相对布局作为一个简单的、可调整大小的框架(而不是利用它的特性)。我最终完全放弃了这种方法。我想我应该回答我自己的问题。
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-25
      • 1970-01-01
      • 2019-02-18
      • 2014-09-11
      • 2017-02-19
      相关资源
      最近更新 更多