【问题标题】:Android Relative Layout - doesn't work like I wantAndroid 相对布局 - 不像我想要的那样工作
【发布时间】:2014-12-09 19:10:11
【问题描述】:

我现在正在尝试这个,但它仍然不起作用。我阅读了很多关于相对布局的教程,但我仍然做不到。请给我一个想法,我能做什么。我想要一个像图片一样的简单布局![在此处输入图片描述][1]

编辑:看起来我无法上传图片... :(

它是一个简单测验应用程序的布局,这些小部件彼此位于下方。一个进度条,一个用于问题的 TextView,4 个用于答案的按钮。 TextView 应该占据其他小部件不需要的所有位置。 Button 应始终位于末尾。

我尝试了 alignParentTop、Bottom、嵌套的 LinearLayouts,但它仍然不起作用。我能做些什么?有一个想法真的很棒吗?非常感谢!

好主意:我上传了图片:http://imgur.com/Z0YmLw2

这是实际代码。 (我编辑了大约 20 次)

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


    <ProgressBar
        android:id="@+id/answerProgressBar"
        style="@android:style/Widget.ProgressBar.Horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/countdownTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/answerProgressBar"
        android:text="00:00:00" />

    <ImageView
        android:id="@+id/questionImageView"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_below="@id/countdownTextView"
        android:layout_margin="5dp"
        android:background="@drawable/ic_launcher"
        android:scaleType="fitCenter" />

    <TextView
        android:id="@+id/questionTextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/answerProgressBar"
        android:layout_margin="5dp"
        android:layout_toRightOf="@id/questionImageView"
        android:text="jfh rhguijfhg fhge hugutbh r rut hruteruzt dkjfh uzdt ggu zguzg zuh ftz huuh rh grhtughrtu thiohjriwetk lkj rgjrjkrjek"
        android:textSize="22dp" />


        <Button
            android:id="@+id/answert1Button"
            style="@style/answerButton"
            android:text="answer 1" />


        <Button
            android:id="@+id/answer2Button"
            style="@style/answerButton"
            android:text="answer 2" />


        <Button
            android:id="@+id/answer3Button"
            style="@style/answerButton"
            android:text="answer 3" />

        <Button
            android:id="@+id/answer4Button"
            style="@style/answerButton"
            android:text="answer 4 Butto d uzrr rd drtd gzugt ffzft drtn" />


</RelativeLayout>

【问题讨论】:

  • 将您的屏幕截图上传到 imgur.com 之类的地方并将其链接到您的帖子中。

标签: android android-layout android-relativelayout


【解决方案1】:

使用RelativeLayout,这是将小部件定位到屏幕的最简单方法但是,您可能需要一些时间才能掌握它。因为如果您使用图形工具设计布局,会更难。

尝试在 XML 文本 中编辑它。请务必阅读this Android Dev Tutorial,了解您可以使用RelativeLayout 访问的所有选项。 我相信您确实检查过,但我也很确定您只是在寻找明显的解决方案。有时,一些不那么明显的选项会引导您实现目标 - 所以请耐心等待,并在教程的帮助下尝试从头开始。

否则,您也可以使用FrameLayout,它将您的屏幕分成几个帧。 但是,同样,它可能会使您的应用程序中的事情变得复杂。

【讨论】:

  • 感谢您的回答。我一遍又一遍地阅读教程,但仍然没有得到它。现在我上传了图片和代码。也许你有一个想法?
  • 对不起,伙计,但从你的代码中我可以看出你真的没有阅读任何教程。你甚至不以任何方式定位你的按钮。去检查一些 Android Widget 的定位操作,例如 android:layout_margin 等。还有很多。我注意到你的按钮上没有使用任何这些。
【解决方案2】:

首先,为了方便,去掉countdownTextView和questionImageView,我们试试吧。

我认为您需要将 layout_alignParentTop 属性与 ProgressBar:

<ProgressBar
    android:id="@+id/answerProgressBar"
    style="@android:style/Widget.ProgressBar.Horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true" />

然后将所有 Button 放在一个垂直的 LinearLayout 中,并带有 layout_alignParentBottom

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

    <Button
        android:id="@+id/answerButton1"
        style="@style/answerButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="answer1" />

    (...)

</LinearLayout>

最后,TextView 必须同时具有 layout_belowlayout_above 属性并设置 layout_height="match_parent"

<TextView
    android:id="@+id/questionTextView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="R.id.answerProgressBar"
    android:layout_above="R.id.answerButtons"
    android:text="blah blah blah" />

【讨论】:

  • 非常感谢您的回答!在 AndroidStudio 中,我无法使用 android:layout_above,因为它显示:错误:(35, 31) 未找到与给定名称匹配的资源(在 'layout_above' 处,值为 '@id/antwortButtonsLinearLayout')。 - 但是当不使用 layout_above 命令时它会起作用
  • 应该是@id/antwortButtonsLinearLayout,但是layout_above。您应该将 layout_above 的 id 指定为 R.id.answerButtonsLinearLayout
猜你喜欢
  • 2013-09-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-16
  • 1970-01-01
相关资源
最近更新 更多