【问题标题】:Android RelativeLayout align concernAndroid RelativeLayout 对齐关注点
【发布时间】:2012-01-22 10:58:39
【问题描述】:

我正在使用 android 布局并尝试进行简单的 QA 测试,但我无法正确设置布局。我只是不知道如何调整这些东西,经过几个小时的战斗,我几乎不需要任何帮助。

这是我想要的:

我得到了什么:

这是我的 XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rootLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<ScrollView
    android:id="@+id/scrollView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true" >

    <RelativeLayout
        android:id="@+id/nodeLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <TextView
            android:id="@+id/textQuestion"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:text="TextView"
            android:textSize="18dp" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:orientation="vertical" >

            <RadioGroup
                android:id="@+id/radioAnswersGroup"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >
            </RadioGroup>

            <LinearLayout
                android:id="@+id/questionsNavigationGroup"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:orientation="horizontal" >

                <Button
                    android:id="@+id/buttonPreviousQuestion"
                    android:layout_width="match_parent"
                    android:layout_height="45dp"
                    android:layout_weight="1"
                    android:background="@drawable/fancy_button"
                    android:gravity="center|center_vertical"
                    android:onClick="onPreviousQuestionButtonClick"
                    android:shadowColor="#fff"
                    android:shadowRadius="3"
                    android:text="Back"
                    android:textColor="#432f11"
                    android:textSize="24dp" />

                <Button
                    android:id="@+id/buttonNextQuestion"
                    android:layout_width="match_parent"
                    android:layout_height="45dp"
                    android:layout_weight="1"
                    android:background="@drawable/fancy_button"
                    android:gravity="center|center_vertical"
                    android:onClick="onNextQuestionButtonClick"
                    android:shadowColor="#fff"
                    android:shadowRadius="3"
                    android:text="Next"
                    android:textColor="#432f11"
                    android:textSize="24dp" />

                <Spinner
                    android:id="@+id/spinner"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="1" />
            </LinearLayout>

        </LinearLayout>

    </RelativeLayout>

</ScrollView>

</LinearLayout>

答案和按钮应始终位于底部,问题应始终位于顶部。但是当文本很长时,视图应该在滚动视图内拉伸。

编辑:

我找到了没有相对布局的方法。我不知道这是错误还是功能,但这种布局可以按我想要的方式缩放。

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textQuestion"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="top" />

    <LinearLayout
        android:id="@+id/bottomStuff"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="bottom"
        android:orientation="vertical" >

        <RadioGroup
            android:id="@+id/radioAnswersGroup"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
        </RadioGroup>

        <LinearLayout
            android:id="@+id/questionsNavigationGroup"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:orientation="horizontal" >

            <Button
                android:id="@+id/buttonPreviousQuestion"
                android:layout_width="match_parent"
                android:layout_height="45dp"
                android:layout_weight="1" />

            <Button
                android:id="@+id/buttonNextQuestion"
                android:layout_width="match_parent"
                android:layout_height="45dp"
                android:layout_weight="1" />

            <Spinner
                android:id="@+id/spinner"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1" />

        </LinearLayout>

    </LinearLayout>

</LinearLayout>

</ScrollView>

【问题讨论】:

  • 您好,您使用哪个工具来创建此屏幕布局? http://i.stack.imgur.com/32wzH.png
  • 你已经为整个布局采用了滚动视图..你为什么不只在滚动视图内包装 textview..所以问题的文本只能滚动 ....
  • 我只是使用 Paint.NET 来绘制图像。为什么我不将 ScrollView 仅用于文本是因为我希望这个问题占据整个空间,如果它太长的话。

标签: android layout scrollview android-relativelayout


【解决方案1】:

将此行添加到您的 TextView 元素:

android:layout_above="@+id/radioAnswersGroup"

【讨论】:

    【解决方案2】:

    试试这个

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout android:id="@+id/RelativeLayout1"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    
    <ScrollView android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:id="@+id/scrollView1"
        android:layout_alignParentTop="true" android:layout_alignParentLeft="true"
        android:layout_above="@+id/radioGroup1"
        android:layout_alignParentRight="true">
    
        <LinearLayout android:id="@+id/linearLayout1"
            android:layout_width="fill_parent" android:layout_height="fill_parent">
            <TextView android:textAppearance="?android:attr/textAppearanceLarge"
                android:layout_width="wrap_content" android:id="@+id/textView1"
                android:text="TextView" android:layout_height="wrap_content" />
        </LinearLayout>
    
    </ScrollView>
    
    <RadioGroup android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:id="@+id/radioGroup1"
        android:layout_above="@+id/spinner1" android:layout_alignParentRight="true"
        android:layout_alignParentLeft="true">
        <RadioButton android:layout_width="wrap_content"
            android:id="@+id/radio0" android:layout_height="wrap_content"
            android:text="RadioButton" android:checked="true" />
        <RadioButton android:layout_width="wrap_content"
            android:id="@+id/radio1" android:layout_height="wrap_content"
            android:text="RadioButton" />
        <RadioButton android:layout_width="wrap_content"
            android:id="@+id/radio2" android:layout_height="wrap_content"
            android:text="RadioButton" />
        <RadioButton android:layout_width="wrap_content"
            android:id="@+id/radio3" android:layout_height="wrap_content"
            android:text="RadioButton" />
    </RadioGroup>
    
    <Button android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:id="@+id/button1"
        android:text="Button" android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true" />
    <Button android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:id="@+id/button2"
        android:text="Button" android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true" />
    <Spinner android:id="@+id/spinner1" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true" />
    
    </RelativeLayout>
    

    【讨论】:

      【解决方案3】:

      您可以尝试为TextView 下方的LinearLayout 提供一个ID,例如android:id="@+id/bottomhalf",并定义android:layout_below="@+id/textQuestion"

      然后到您的TextView 添加android:layout_above="@+id/bottomhalf"

      【讨论】:

        【解决方案4】:

        我认为以下更改将解决问题,

        <ScrollView
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fillViewport="true" >
        

        而不是,

        <ScrollView
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true" >
        

        希望对你有帮助..

        【讨论】:

          【解决方案5】:

          使用以下布局,它会产生您在第一个链接中提到的输出。修改间距和可绘制对象。希望对你有帮助。

          我在纵向模式下使用800X480分辨率检查输出,您必须根据您的设备分辨率修改布局宽度和高度。

          <?xml version="1.0" encoding="utf-8"?>
          <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/rootLayout"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:orientation="vertical" >
          
              <ScrollView
                  android:id="@+id/scrollView"
                  android:layout_width="match_parent"
                  android:layout_height="300px"
                  android:fillViewport="true" >
          
                  <TextView
                      android:id="@+id/textQuestion"
                      android:layout_width="fill_parent"
                      android:layout_height="fill_parent"
                      android:layout_alignParentTop="true"
                      android:text="TextView"
                      android:textSize="18dp" />
              </ScrollView>
              <RadioGroup
                  android:id="@+id/radioAnswersGroup"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:layout_alignParentLeft="true">
                  <RadioButton android:layout_width="wrap_content"
                      android:id="@+id/radio0" android:layout_height="wrap_content"
                      android:text="RadioButton" android:checked="true" />
                  <RadioButton android:layout_width="wrap_content"
                      android:id="@+id/radio1" android:layout_height="wrap_content"
                      android:text="RadioButton" />
                  <RadioButton android:layout_width="wrap_content"
                      android:id="@+id/radio2" android:layout_height="wrap_content"
                      android:text="RadioButton" />
                  <RadioButton android:layout_width="wrap_content"
                      android:id="@+id/radio3" android:layout_height="wrap_content"
                      android:text="RadioButton" />               
              </RadioGroup>
          
              <LinearLayout
                  android:id="@+id/questionsNavigationGroup"
                  android:layout_width="match_parent"
                  android:layout_height="50dp"
                  android:orientation="horizontal" >
          
                  <Button
                      android:id="@+id/buttonPreviousQuestion"
                      android:layout_width="match_parent"
                      android:layout_height="45dp"
                      android:layout_weight="1"
                      android:gravity="center|center_vertical"
                      android:onClick="onPreviousQuestionButtonClick"
                      android:shadowColor="#fff"
                      android:shadowRadius="3"
                      android:text="Back"
                      android:textColor="#432f11"
                      android:textSize="24dp" />
          
                  <Button
                      android:id="@+id/buttonNextQuestion"
                      android:layout_width="match_parent"
                      android:layout_height="45dp"
                      android:layout_weight="1"                    
                      android:gravity="center|center_vertical"
                      android:onClick="onNextQuestionButtonClick"
                      android:shadowColor="#fff"
                      android:shadowRadius="3"
                      android:text="Next"
                      android:textColor="#432f11"
                      android:textSize="24dp" />
                  <Spinner
                      android:id="@+id/spinner"
                      android:layout_width="match_parent"
                      android:layout_height="match_parent"
                      android:layout_weight="1" />
              </LinearLayout>
          
          </LinearLayout>
          

          【讨论】:

            【解决方案6】:

            我找到了没有相对布局的方法。我不知道这是错误还是功能,但这种布局可以按我想要的方式缩放。

            <?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:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical" >
            
                <TextView
                    android:id="@+id/textQuestion"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="top" />
            
                <LinearLayout
                    android:id="@+id/bottomStuff"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="bottom"
                    android:orientation="vertical" >
            
                    <RadioGroup
                        android:id="@+id/radioAnswersGroup"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content" >
                    </RadioGroup>
            
                    <LinearLayout
                        android:id="@+id/questionsNavigationGroup"
                        android:layout_width="match_parent"
                        android:layout_height="50dp"
                        android:orientation="horizontal" >
            
                        <Button
                            android:id="@+id/buttonPreviousQuestion"
                            android:layout_width="match_parent"
                            android:layout_height="45dp"
                            android:layout_weight="1" />
            
                        <Button
                            android:id="@+id/buttonNextQuestion"
                            android:layout_width="match_parent"
                            android:layout_height="45dp"
                            android:layout_weight="1" />
            
                        <Spinner
                            android:id="@+id/spinner"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:layout_weight="1" />
            
                    </LinearLayout>
            
                </LinearLayout>
            
            </LinearLayout>
            
            </ScrollView>
            

            【讨论】:

              【解决方案7】:

              我认为你应该使用这个:

              <ScrollView
              android:id="@+id/scrollView"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:fillViewport="true" >
              

              【讨论】:

                【解决方案8】:

                实际上,我一直在从事一个类似的项目,该项目具有您的一些类似要求。 我在窗格上同时拥有微调器、按钮图像视图等

                请检查下面的 xml 布局代码并尝试更改它的一些参数以进行相应的调整。

                <?xml version="1.0" encoding="utf-8"?>
                <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="fill_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:padding="3sp" >
                
                <LinearLayout
                    android:id="@+id/linearLayout"
                    android:layout_width="fill_parent"
                    android:layout_height="0dp"
                    android:layout_weight="1"
                    android:baselineAligned="true"
                    android:orientation="horizontal" >
                
                    <ScrollView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1" >
                
                        <RelativeLayout
                            android:id="@+id/innerRelativeLayout"
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content" >
                
                            <ImageView
                                android:id="@+id/imgStartStop"
                                android:layout_width="50dp"
                                android:layout_height="50dp"
                                android:layout_marginLeft="10dp"
                                android:layout_marginTop="20dp"
                                android:contentDescription="@string/startstop"
                                android:onClick="startstop"
                                android:src="@drawable/start" />
                
                            <Button
                                android:id="@+id/btnStatus"
                                android:layout_width="220dp"
                                android:layout_height="75dp"
                                android:layout_marginLeft="5dp"
                                android:layout_marginRight="5dp"
                                android:layout_marginTop="10dp"
                                android:layout_toRightOf="@id/imgStartStop"
                                android:onClick="showStatus"
                                android:text="@string/ontime" />
                
                            <ImageView
                                android:id="@+id/imgPushToTalk"
                                android:layout_width="100dp"
                                android:layout_height="100dp"
                                android:layout_below="@id/imgStartStop"
                                android:layout_centerHorizontal="true"
                                android:layout_marginTop="25dp"
                                android:contentDescription="@string/pushtotalk"
                                android:onClick="activatePushtoTalk"
                                android:src="@drawable/pushtotalkidle" />
                
                            <TextView
                                android:id="@+id/txtExpectedNumberofPassengers"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_below="@id/imgPushToTalk"
                                android:layout_centerHorizontal="true"
                                android:layout_marginTop="5dp"
                                android:text="@string/expectednumberofpassengers"
                                android:textSize="30sp" />
                
                            <TextView
                                android:id="@+id/txtNumberofPassengers"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_below="@id/imgPushToTalk"
                                android:layout_marginTop="5dp"
                                android:layout_toLeftOf="@id/txtExpectedNumberofPassengers"
                                android:text="@string/numberofpassengers"
                                android:textColor="@color/greenText"
                                android:textSize="30sp" />
                        </RelativeLayout>
                    </ScrollView>
                
                    <fragment
                        android:id="@+id/mapFragment"
                        android:name="com.google.android.gms.maps.MapFragment"
                        android:layout_width="0dp"
                        android:layout_height="fill_parent"
                        android:layout_weight="3" />
                </LinearLayout>
                
                <TextView
                    android:id="@+id/txtTickerMessage"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:ellipsize="marquee"
                    android:focusable="true"
                    android:focusableInTouchMode="true"
                    android:marqueeRepeatLimit="marquee_forever"
                    android:scrollHorizontally="true"
                    android:singleLine="true"
                    android:text="@string/tickermessage"
                    android:textAppearance="?android:attr/textAppearanceLarge" />
                
                </LinearLayout>
                

                【讨论】:

                  【解决方案9】:

                  这将起作用:

                  <?xml version="1.0" encoding="utf-8"?>
                  <ScrollView
                  xmlns:android="http://schemas.android.com/apk/res/android"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content">
                  
                  <LinearLayout
                  android:orientation="vertical"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent">
                  
                  <LinearLayout
                      android:layout_width="match_parent"
                      android:layout_height="match_parent"
                      android:layout_weight="1"
                      android:id="@+id/linear1">
                  
                      <TextView
                          android:layout_width="match_parent"
                          android:layout_height="wrap_content"
                          android:text="text"/>
                  </LinearLayout>
                  
                  <LinearLayout
                      android:layout_width="match_parent"
                      android:layout_height="match_parent"
                      android:layout_weight="1"
                      android:id="@+id/linear2">
                  
                      <RadioGroup android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:id="@+id/radioGroup1">
                          <RadioButton
                              android:layout_width="wrap_content"
                              android:id="@+id/radio0"
                              android:layout_height="wrap_content"
                              android:text="RadioButton"
                              android:checked="true" />
                  
                          <RadioButton
                              android:layout_width="wrap_content"
                              android:id="@+id/radio1"
                              android:layout_height="wrap_content"
                              android:text="RadioButton" />
                  
                          <RadioButton
                              android:layout_width="wrap_content"
                              android:id="@+id/radio2"
                              android:layout_height="wrap_content"
                              android:text="RadioButton" />
                  
                          <RadioButton
                              android:layout_width="wrap_content"
                              android:id="@+id/radio3"
                              android:layout_height="wrap_content"
                              android:text="RadioButton" />
                  
                      </RadioGroup>
                  
                  </LinearLayout>
                  
                  <LinearLayout
                      android:layout_width="match_parent"
                      android:layout_height="match_parent"
                      android:layout_weight="1"
                      android:id="@+id/linear3">
                  
                      <Button
                          android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:id="@+id/button1"
                          android:text="Button" />
                  
                      <Button
                          android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:id="@+id/button2"
                          android:text="Button" />
                  
                      <Spinner android:id="@+id/spinner1"
                          android:layout_width="wrap_content"
                          android:layout_height="wrap_content" />
                  
                  </LinearLayout>
                  
                  </LinearLayout>
                  </ScrollView>
                  

                  【讨论】:

                    猜你喜欢
                    • 1970-01-01
                    • 1970-01-01
                    • 2015-03-03
                    • 2016-07-20
                    • 2011-08-03
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 2014-10-03
                    相关资源
                    最近更新 更多