【问题标题】:How to fill the screen with only one element?如何只用一个元素填充屏幕?
【发布时间】:2013-09-26 11:58:27
【问题描述】:

我想在屏幕上创建一个包含 2 个元素的活动,一个在底部,具有固定大小,一个在其顶部,必须填满屏幕。

如何在布局中做到这一点?

谢谢

【问题讨论】:

    标签: android layout size fixed fill


    【解决方案1】:

    layout_weight="1" 用于第一个元素并放入layout_height="0dp" 并使用linearlayout 作为包含2 个元素的父元素。

    【讨论】:

      【解决方案2】:

      使用下面的代码:

      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical"
          tools:context=".MainActivity" >
      
          <TextView
              android:id="@+id/textView1"
              android:layout_width="match_parent"
              android:layout_height="0dp"
              android:layout_weight="1"
              android:gravity="center"
              android:text="@string/hello_world" />
      
          <TextView
              android:id="@+id/textView2"
              android:layout_width="match_parent"
              android:layout_height="50dp"
              android:gravity="center"
              android:text="test" />
      
      
      
      </LinearLayout>
      

      【讨论】:

        【解决方案3】:

        或者你可以这样做,

        <LinearLayout
            android:id="@+id/linearLayout1"
            android:layout_width="fill_parent"
            android:layout_alignParentTop="true"
            android:layout_height="wrap_content"
               android:orientation="horizontal"
               android:layout_above="@+id/ll1"
         >
        </LinearLayout>
        
        <LinearLayout
            android:id="@+id/ll1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
        
            android:layout_alignParentBottom="true"
        
            android:orientation="horizontal"
            >
        </LinearLayout>
        

        【讨论】:

          【解决方案4】:

          将此添加到第一个元素:

            android:layout_weight="0.8"
          

          第二个

          android:layout_weight="0.2"
          

          【讨论】:

            【解决方案5】:

            您可以使用相对布局,例如...以下示例

            <//you are second element
                android:id="@+id/ui_radiogroup"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true"
                android:clickable="false"
                android:orientation="horizontal" >
            <///you are second element>
            
            <//you are First element
                android:id="@+id/ui_pager"
                android:layout_width="wrap_content"
                android:layout_above="@id/ui_radiogroup"
                android:layout_height="wrap_content"
                android:scrollbars="horizontal" />
            

            其他选项使用线性布局根据您的要求将 android:layout_weight 分配给每个元素

            【讨论】:

              【解决方案6】:

              试试这个,

              <?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"
               >
              
              <TextView
                  android:id="@+id/footertext"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:layout_alignParentBottom="true"
                  android:gravity="center"
                  android:text="Footer" />
              
              <TextView
                  android:id="@+id/centertext"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:layout_above="@id/footertext"
                  android:gravity="center"
                  android:text="center" />
              

              【讨论】:

                【解决方案7】:

                这是完整的技巧:

                • 使用RelativeLayout 包装您的两个项目,
                • layout_alignParentBottom="true"放在你的底部物品上
                • 还有layout_above="@+id/your_bottom_item_id" 在您的顶部项目上

                这是一个完整的工作示例,您可以 cpy/paste :

                <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" >
                
                    <LinearLayout 
                        android:id="@+id/top_item"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:orientation="vertical"
                        android:layout_above="@+id/bottom_layout">
                    </LinearLayout>
                
                    <LinearLayout 
                        android:id="@+id/bottom_item"
                        android:layout_width="match_parent"
                        android:layout_height="50dp"
                        android:orientation="vertical"
                        android:layout_alignParentBottom="true">
                    </LinearLayout>
                
                </RelativeLayout>
                

                【讨论】:

                  【解决方案8】:

                  只需将 layout_weight="1" 用于第一个元素,布局高度应为 match_parent。

                  【讨论】:

                    猜你喜欢
                    • 1970-01-01
                    • 2012-03-31
                    • 1970-01-01
                    • 2021-03-25
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 2022-01-01
                    • 2021-05-05
                    相关资源
                    最近更新 更多