【问题标题】:Inflate a view between elements in a view (Android XML)在视图中的元素之间膨胀视图(Android XML)
【发布时间】:2012-09-04 17:45:41
【问题描述】:

我想放大一些视图,但视图底部有一个按钮。我已经知道如何动态膨胀视图,然后膨胀其他膨胀视图下方的按钮。但是,当只有几个膨胀视图(导致组合视图占用的空间少于整个屏幕)时,按钮不会像我想要的那样位于页面底部。我的想法是,我需要通过将按钮设置为:android:layout_alignParentBottom(当我尝试执行此操作时,尽管应用程序崩溃)来使按钮在相对布局的底部对齐

这是我所拥有的屏幕截图,然后是我想要的屏幕截图,您看到的所有项目都被膨胀到相对布局内的滚动视图上。我认为问题在于按钮被充气到滚动视图上,而滚动视图的大小仅在项目强制它的情况下(不幸的是我太菜鸟不知道如何充气到视图的不同部分)

左侧图像的代码如下(我没有左侧视图的错误): 父视图:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@drawable/blurybg"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">    
<ScrollView     
  android:layout_height="wrap_content" 
  android:id="@+id/scrollView1" 
  android:layout_width="fill_parent" >
<LinearLayout     
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:id= "@+id/parent_of_inflated_view_id">
</LinearLayout>   
</ScrollView>
</RelativeLayout>

childButton(其他视图与此类似)

<?xml version="1.0" encoding="utf-8"?>
<Button
xmlns:android="http://schemas.android.com/apk/res/android"
android:height="30dp"
android:layout_width="fill_parent"
android:background="@drawable/done"
android:layout_alignBottom ="@layout/availablerestaurants"
/>

用于创建充气机和充气按钮的 Java。其他充气视图与此类似(我当然使用相同的充气机)。我最后添加了充气按钮以使其位于视图的底部。

LayoutInflater theInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
LinearLayout linLayout = (LinearLayout)findViewById(R.id.parent_of_inflated_view_id); 

    Button doneButtonCurrentRest=  (Button) theInflater.inflate(R.layout.done_button_availrest, null);
    linLayout.addView(doneButtonCurrentRest);

感谢您的帮助, 亚当

【问题讨论】:

  • 一段代码将有助于指出您的错误。
  • 另外,“android:layout_alignParentBottom”为什么失败也很不清楚。请提供一些异常日志。
  • 仅供参考:当我尝试对齐父底部时,我没有膨胀按钮,我只是将按钮放在父视图 xml 中并将其设置为对齐相对布局的父底部。我想我不能在按钮和视图顶部之间膨胀我的其他视图?

标签: android xml android-layout android-xml


【解决方案1】:

请检查我的代码。它应该按预期工作:

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

    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:id="@+id/parent_of_inflated_view_id"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" />
    </ScrollView>

    <Button
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:layout_alignParentBottom="true" />

</RelativeLayout>

你的代码呢。我可以看到几个错误:

1) 您应该为您的按钮指定android:layout_height。没有这个参数会抛出异常。您使用android:height,但它与android:layout_height 不同。

2) android:layout_alignBottom 应该指向 id,而不是布局。此外,我在按钮的单独布局中看不到此属性的用途。

更新

这是下面评论中问题的解决方案:

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

    <LinearLayout
        android:id="@+id/layout_for_positioning"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <LinearLayout
            android:id="@+id/parent_of_inflated_view_id"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical" />

        <Button
            android:id="@+id/button"
            android:layout_width="match_parent"
            android:layout_height="30dp"
            android:layout_weight="0" />
    </LinearLayout>

</ScrollView>

请注意,我将android:fillViewport="true" 用于 ScrollView,因此它可以填满整个屏幕。我还使用android:layout_weight 正确拉伸视图(您可以找到属性here 的解释)。

【讨论】:

  • 好吧,我使用了你的代码(我实际上最初使用了类似的东西,在滚动视图和我将按钮膨胀到的 relLayout 之间有一个 lin 布局)。我对这两种解决方案的问题是,在我添加了很多膨胀的视图之后,“完成按钮”总是在页面上。有没有办法让我拥有它,这样我就不会看到“完成按钮”,直到我滚动过去所有那些膨胀的视图?我非常感谢您的评论,它非常接近我认为的正确答案
  • 但同时您希望按钮位于屏幕末尾,如第二张截图所示?让我想想。
  • 为什么你在另一个线性布局里面有一个线性布局?
  • 第一个 LinearLayout 用于正确放置按钮:按钮将位于屏幕底部,当有太多膨胀元素时,它将位于它们下方(您应该滚动它)。我认为用一种布局很难达到同样的效果。此外,这种结构看起来更合乎逻辑:充气项目的单独布局与控制元素(按钮)没有共同点
  • 首先我使用 android:fillViewport="true" 让 ScrollView 匹配他的父级。我不知道为什么,但 android:layout_height="match_parent" 对于 ScrollView 没有按预期工作。可能是因为这个视图的实际长度不同。其次,我使用 android:layout_weight 为 id/layout_for_positioning 的子视图设置比例。尝试更改一些值,看看会发生什么。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多