草庐IT

android - RelativeLayout 高度填充剩余空间

coder 2023-11-18 原文

我的 xml 文件中有以下布局:

<RelativeLayout android:layout_width="fill_parent"
                android:layout_height="fill_parent">

    <FrameLayout android:id="@+id/logoLayout"  
                 android:layout_width="fill_parent"
             android:layout_height="wrap_content">
          -- some images
    </FrameLayout>


    <RelativeLayout android:layout_width="fill_parent" 
                    android:layout_height="wrap_content" 
                    android:gravity="center" 
                    android:orientation="vertical"
                    android:layout_below="@+id/logoLayout">

               Button 1
               Button 2
               Button 3
               Button 4

    </RelativeLayout>

    <RelativeLayout android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_margin="5dip"
            android:layout_alignParentBottom="true">

                 Button 5
    </RelativeLayout>

<RelativeLayout>

也许我没有以最好的方式做到这一点。我想要的:拥有包含 4 个按钮的布局以使用顶部和底部布局之间的整个空间,并且我希望按钮在布局中均匀排列。

是这样的:http://img16.imageshack.us/i/androidq.png/

我添加了整个布局代码:

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


    <!--The header of the page-->
        <FrameLayout android:id="@+id/logoLayout" 
                     android:layout_width="fill_parent"
                     android:layout_height="wrap_content">        

             <ImageView android:id="@+id/logoBackground" 
                        android:src="@drawable/logo_background_small"
                        android:layout_width="fill_parent" 
                        android:layout_height="wrap_content"/>

             <ImageView android:id="@+id/logoImage" 
                        android:src="@drawable/logo_small"
                        android:layout_width="wrap_content" 
                        android:layout_height="wrap_content" 
                        android:layout_gravity="left"
                        android:gravity="center"
                        android:padding="3dip"/>  

             <TextView android:layout_width="fill_parent" 
                       android:layout_height="wrap_content" 
                       android:text="@string/tracks"
                       android:layout_gravity="center"
                       android:gravity="right"
                       android:textSize="22dip"
                       android:textColor="#ffffff"
                       android:padding="3dip">
             </TextView>              

        </FrameLayout>


        <RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android"
                         android:layout_width="fill_parent" 
                         android:layout_height="wrap_content" 
                         android:gravity="center" 
                         android:orientation="vertical"
                         android:layout_below="@+id/logoLayout"> 

                            <Button android:id="@+id/btn1"
                                    android:layout_height="wrap_content" 
                                    android:layout_width="250dip" 
                                    android:drawableLeft="@drawable/img_small_btn_look_around"
                                    android:background="@drawable/main_long_menu_button"
                                    android:text="@string/btn1" 
                                    android:textSize="18dip"
                                    android:textColor="#ffffff"
                                    android:layout_marginTop="20dip"
                                    android:onClick="btnMyTracksOnClick">
                            </Button> 

                            <Button android:id="@+id/btn2"
                                    android:layout_height="wrap_content" 
                                    android:layout_width="250dip" 
                                    android:drawableLeft="@drawable/img_small_btn_look_around"
                                    android:background="@drawable/main_long_menu_button"
                                    android:text="@string/btn2" 
                                    android:textSize="18dip"
                                    android:textColor="#ffffff"
                                    android:layout_marginTop="20dip"
                                    android:layout_below="@+id/btn1">
                            </Button>

                            <Button android:id="@+id/btn3"
                                    android:layout_height="wrap_content" 
                                    android:layout_width="250dip" 
                                    android:drawableLeft="@drawable/img_small_btn_look_around"
                                    android:background="@drawable/main_long_menu_button"
                                    android:text="@string/btn3" 
                                    android:textSize="18dip"
                                    android:textColor="#ffffff"
                                    android:layout_marginTop="20dip"
                                    android:layout_below="@+id/btn2">
                            </Button>

                            <Button android:id="@+id/btn4"
                                    android:layout_height="wrap_content" 
                                    android:layout_width="250dip" 
                                    android:drawableLeft="@drawable/img_small_btn_look_around"
                                    android:background="@drawable/main_long_menu_button"
                                    android:text="@string/btn4" 
                                    android:textSize="18dip"
                                    android:textColor="#ffffff"
                                    android:layout_marginTop="20dip"
                                    android:layout_below="@+id/bt3">
                            </Button>
        </RelativeLayout>

        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                        android:orientation="horizontal"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:layout_margin="5dip"
                        android:layout_alignParentBottom="true">

                        <Button android:layout_width="90dip" 
                                android:layout_height="wrap_content"
                                android:textSize="20dip"
                                android:textColor="#ffffff"
                                android:layout_alignParentLeft="true"
                                android:background="@drawable/sett_menu_button"
                                android:text="@string/back" 
                                android:layout_marginLeft="3dip"/> 

         </RelativeLayout>

    </RelativeLayout>

最佳答案

相对布局方法

我遇到过类似的情况,但我使用 RelativeLayout 而不是 LinearLayout 让它工作。

我基本上有 2 个标签( TextView )。顶部标签应填充剩余空间,底部标签是一段文本,应环绕其内容但仍固定在容器底部。这两个 View 都在 RelativeLayout 容器中

所以基本上:

------- parent container top ------------
|
|
|
[label1 (auto expand)]
|
|
|
[label2 (fixed height)]
------- parent container bottom ------------

技术

  1. 对于标签 1,我将宽度和高度的布局参数设置为 MATCH_PARENT,以便它自动展开。
  2. 为标签 1 添加规则,使其位于 ABOVE 标签 2(这是重要部分)

然后对于标签 2:

  1. 使用 MATCH_PARENT 作为宽度,使用 WRAP_CONTENT 作为高度
  2. 然后将标签 2 的规则添加到 ALIGN_PARENT_BOTTOM

这个布局指令告诉Android系统:

标签 2 应包裹其内容并固定到父容器的底部,同时,标签 1 应填充标签 2 上方的剩余空间。

要记住的重要一点是设置为 ALIGN_PARENT_SOMETHING 的 View ,例如ALIGN_PARENT_TOPALIGN_PARENT_BOTTOM 是“优先 View ”。应相对于此“优先 View ”设置应填充剩余空间的所有其他 View 。

基本示例

这是一个使用编程方法的演示:

package com.zhang.relodemo;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    // ---------------------------------------------------
    // Member Variables
    // ---------------------------------------------------

    RelativeLayout container;

    TextView label1;
    TextView label2;

    // ---------------------------------------------------
    // Property Methods
    // ---------------------------------------------------

    public RelativeLayout getContainer() {
        return container;
    }

    public void setContainer(RelativeLayout container) {
        this.container = container;
    }

    public TextView getLabel1() {
        return label1;
    }

    public void setLabel1(TextView label1) {
        this.label1 = label1;
    }

    public TextView getLabel2() {
        return label2;
    }

    public void setLabel2(TextView label2) {
        this.label2 = label2;
    }

    // ---------------------------------------------------
    // Methods
    // ---------------------------------------------------

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initViews();
        initLayouts();
        addViews();
    }

    void initViews() {
        container = new RelativeLayout(this);

        label1 = new TextView(this);
        label1.setText("The quick brown fox jumped over the lazy dog.");
        label1.setBackgroundColor(Color.LTGRAY);
        label1.setGravity(Gravity.CENTER);
        label1.setTextSize(20);

        label2 = new TextView(this);
        label2.setText("Label 2 is a short label compared to label1.It has two lines but take up a fraction of the space.");
        label2.setBackgroundColor(Color.GRAY);
        label2.setTextSize(20);
        label2.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
    }

    void initLayouts() {
        container.setId(View.generateViewId());

        label1.setId(View.generateViewId());
        label2.setId(View.generateViewId());


        // ---------------------------------------------------
        // Parent Container Constraints
        // ---------------------------------------------------

        RelativeLayout.LayoutParams containerLayoutParams = new RelativeLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT
        );

        container.setLayoutParams(containerLayoutParams);



        // ---------------------------------------------------
        // Label Constraints
        // ---------------------------------------------------

        RelativeLayout.LayoutParams label1LayoutParams = new RelativeLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT
        );

        // --------------------------------------
        // THIS IS THE IMPORTANT STEP HERE
        //
        // It tells label1 to be above label2
        // while expanding vertically.
        // --------------------------------------
        label1LayoutParams.addRule(RelativeLayout.ABOVE, label2.getId());

        label1.setLayoutParams(label1LayoutParams);




        RelativeLayout.LayoutParams label2LayoutParams = new RelativeLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT
        );

        // ------------------------------------------
        // this pins label2 to the parent
        // container's bottom, preventing label1
        // from forcing label2 off bottom of screen
        // ------------------------------------------
        label2LayoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);

        label2.setLayoutParams(label2LayoutParams);
    }

    void addViews() {
        getContainer().addView(getLabel1());
        getContainer().addView(getLabel2());

        this.setContentView(getContainer());
    }
}

或其 XML 等价物:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/container">
    <TextView
        android:id="@+id/label1"
        android:text="The quick brown fox jumped over the lazy dog."
        android:background="#ccc"
        android:gravity="center"
        android:textSize="20sp"
        android:textAlignment="center"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/label2" />

    <TextView
        android:id="@+id/label2"
        android:text="Label 2 is a short label compared to label1.It has two lines but take up a fraction of the space."
        android:background="@android:color/darker_gray"
        android:textSize="20sp"
        android:textAlignment="center"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"/>
</RelativeLayout>

应该给你以下结果:

关于android - RelativeLayout 高度填充剩余空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3676095/

有关android - RelativeLayout 高度填充剩余空间的更多相关文章

  1. ruby - 什么是填充的 Base64 编码字符串以及如何在 ruby​​ 中生成它们? - 2

    我正在使用的第三方API的文档状态:"[O]urAPIonlyacceptspaddedBase64encodedstrings."什么是“填充的Base64编码字符串”以及如何在Ruby中生成它们。下面的代码是我第一次尝试创建转换为Base64的JSON格式数据。xa=Base64.encode64(a.to_json) 最佳答案 他们说的padding其实就是Base64本身的一部分。它是末尾的“=”和“==”。Base64将3个字节的数据包编码为4个编码字符。所以如果你的输入数据有长度n和n%3=1=>"=="末尾用于填充n%

  2. ruby - 匹配大写字母并用后续字母填充,直到一定的字符串长度 - 2

    我有一个驼峰式字符串,例如:JustAString。我想按照以下规则形成长度为4的字符串:抓取所有大写字母;如果超过4个大写字母,只保留前4个;如果少于4个大写字母,则将最后大写字母后的字母大写并添加字母,直到长度变为4。以下是可能发生的3种情况:ThisIsMyString将产生TIMS(大写字母);ThisIsOneVeryLongString将产生TIOV(前4个大写字母);MyString将生成MSTR(大写字母+tr大写)。我设法用这个片段解决了前两种情况:str.scan(/[A-Z]/).first(4).join但是,我不太确定如何最好地修改上面的代码片段以处理最后一种

  3. ruby-on-rails - 从应用程序中自定义文件夹内的命名空间自动加载 - 2

    我们目前正在为ROR3.2开发自定义cms引擎。在这个过程中,我们希望成为我们的rails应用程序中的一等公民的几个类类型起源,这意味着它们应该驻留在应用程序的app文件夹下,它是插件。目前我们有以下类型:数据源数据类型查看我在app文件夹下创建了多个目录来保存这些:应用/数据源应用/数据类型应用/View更多类型将随之而来,我有点担心应用程序文件夹被这么多目录污染。因此,我想将它们移动到一个子目录/模块中,该子目录/模块包含cms定义的所有类型。所有类都应位于MyCms命名空间内,目录布局应如下所示:应用程序/my_cms/data_source应用程序/my_cms/data_ty

  4. 安卓apk修改(Android反编译apk) - 2

    最近因为项目需要,需要将Android手机系统自带的某个系统软件反编译并更改里面某个资源,并重新打包,签名生成新的自定义的apk,下面我来介绍一下我的实现过程。APK修改,分为以下几步:反编译解包,修改,重打包,修改签名等步骤。安卓apk修改准备工作1.系统配置好JavaJDK环境变量2.需要root权限的手机(针对系统自带apk,其他软件免root)3.Auto-Sign签名工具4.apktool工具安卓apk修改开始反编译本文拿Android系统里面的Settings.apk做demo,具体如何将apk获取出来在此就不过多介绍了,直接进入主题:按键win+R输入cmd,打开命令窗口,并将路

  5. ruby - 如何用递增的值填充数组 Ruby - 2

    我正在尝试解决http://projecteuler.net/problem=1.我想创建一个方法,它接受一个整数,然后创建一个包含它前面的所有整数的数组,并将整数本身作为数组中的值。以下是我目前所拥有的。代码不起作用。defmake_array(num)numbers=Array.newnumcount=1numbers.eachdo|number|numbers 最佳答案 (1..num).to_a是您在Ruby中需要做的全部。1..num将创建一个Range对象,以1开始并以任意值num结束是。Range对象有to_a方法通过

  6. ruby-on-rails - RoR中是否有任何内置方法可以为整数填充零? - 2

    如果我想要“00001”而不是“1”,除了我自己写填零方法之外,有没有内置的方法可以帮助我为整数填零? 最佳答案 puts"%05d"%1#00001参见:String::%,Kernel::sprintf这是正在发生的事情。%左侧的"%05d"是C风格的格式说明符。%右边的变量就是要格式化的东西。格式说明符可以像这样解码:%-格式说明符的开头0-用前导零填充5-长度为5个字符d-被格式化的是一个整数如果你要格式化多个东西,你会把它们放在一个数组中:"%d-%s"%[1,"One"]#=>1-one

  7. ruby-on-rails - 用一系列时间增量填充选择,加上其他选项 - 2

    使用RubyonRails,我使用给定的增量(例如每30分钟)用时间填充“选择”。目前我正在YAML文件中写出所有的可能性,但我觉得有一种更巧妙的方法。我想我想提供一个开始时间、一个结束时间、一个增量,并且目前只提供一个名为“关闭”的选项(想想“business_hours”)。所以,我的选择可能会显示:'Closed'5:00am5:30am6:00am...[allthewayto]...11:30pm谁能想出更好的方法,或者只是将它们全部“拼写”出来的最佳方法? 最佳答案 此答案基于@emh的答案。defcreate_hour

  8. ruby 认为我在引用一个顶级常量,即使我指定了完整的命名空间 - 2

    在我的应用程序中我有classUserincludeUser::FooendUser::Foo定义在app/models/user/foo.rb现在我正在使用一个定义了自己的Foo类的库。我收到此错误:warning:toplevelconstantFooreferencedbyUser::FooUser仅引用具有完整路径的Foo,User::Foo,而Foo实际上从来没有指的是Foo。这是怎么回事?更新:才想起我之前遇到过同样的问题,在问题1中看到这里:HowdoIrefertoasubmodule's"fullpath"inruby? 最佳答案

  9. Ruby 命名空间与类还是模块? - 2

    考虑Ruby类Foo::Bar。惯例是将“Foo”命名空间作为一个模块,但它也可以很容易地作为一个类:moduleFoo;classBar;end;end对比:classFoo;classBar;end;end在第二种情况下,Bar不是Foo的内部类,它只是在Foo的单例上定义的另一个常量。在这两种情况下,父类(superclass)都是Object并且它们只包含Kernel模块。它们的祖先链是相同的。因此,除了您可以根据其类使用Foo进行的操作(如果是类则实例化,如果是模块则扩展/包含),命名空间的性质是否对有任何影响酒吧?是否有令人信服的理由选择其中一个名称间距而不是另一个?我看到

  10. ruby - 如何在命名空间类中包含模块? - 2

    我在将模块包含在命名空间类中时遇到问题。下面的示例抛出错误uninitializedconstantBar::Foo::Baz(NameError)。我在这里缺少哪些基本的Ruby知识?moduleFoomoduleBazdefhelloputs'hello'endendendmoduleBarclassFooincludeFoo::Bazendendfoo=Bar::Foo.new 最佳答案 使用::强制查找到顶层:moduleBarclassFooinclude::Foo::Bazendend

随机推荐