【问题标题】:In android how to create footer with three buttons?在android中如何用三个按钮创建页脚?
【发布时间】:2013-03-19 07:00:31
【问题描述】:

伙计们,我需要用三个按钮制作一个页脚,这样左右按钮应该从左右占据屏幕的 25%,而居中的图像应该占据屏幕的 50% 而不会留下任何它们之间的空间。

如果有人能提供帮助,那就太好了。

我想发布所需页脚的图片,但我不允许分享图片。

【问题讨论】:

  • 您可以使用tabhost并将其位置设置在底部。有一个参考here。但它不鼓励使用像 iphone 这样的底部标签。

标签: android android-layout


【解决方案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" >

    <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:weightSum="4" android:layout_alignParentBottom="true">
        <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="hello"/>
        <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="2" android:text="hello"/>
        <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="hello"/>
    </LinearLayout>
    </RelativeLayout>

看起来是这样的:

【讨论】:

  • 这就是我想要的,我对你的代码做了一些修改,它对我有用。谢谢。
【解决方案2】:

如果您想在所有活动中自定义底部栏,那么您需要在所有活动中包含布局。 我认为下面的代码可能会对您有所帮助:

  1. 在布局目录中创建bottomlayout.xml,并在其中复制下面的代码并使用适当的图像。

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:background="@android:color:black"
        android:gravity="center_horizontal" >
    
        <ImageButton
            android:id="@+id/btn_1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="2dp"
            android:background="@drawable/img1" />
    
        <ImageButton
            android:id="@+id/btn_2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="15dp"
            android:layout_marginTop="2dp"
            android:background="@drawable/img2" />
    
        <ImageButton
            android:id="@+id/btn_3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="15dp"
            android:layout_marginTop="2dp"
            android:background="@drawable/img3" />
    
    </LinearLayout>
    

  2. 在底部的所有活动中包含上述 xml,例如:

  3. 在所有 .Java 文件中编写下面的代码,在 .xml 布局文件中包含 bottonlayout。

    ImageButton btn1=(ImageButton)findViewById(R.id.btn_1);
    ImageButton btn2=(ImageButton)findViewById(R.id.btn_2);
    ImageButton btn3=(ImageButton)findViewById(R.id.btn_3);
    
    btn1.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View v)
        {
            // Your Code Here....
        }
    });
    
    btn2.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View v)
        {
            // Your Code Here....
        }
    });
    
    btn3.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View v)
        {
            // Your Code Here....
        }
    });
    

【讨论】:

    【解决方案3】:

    如下编写布局 XML。

     <LinearLayout  
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:weightSum="4">
        <Button android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:text="25%"
        android:layout_weight="1"/>
        <Button android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:text="50%"
        android:layout_weight="2"/>
        <Button android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:text="25%"
        android:layout_weight="1"/>
    </LinearLayout>
    

    线性布局应放置在相对布局中,使其与父底部对齐。

        android:layout_alignParentBottom="true"
    

    所以你的最终布局将类似于以下

    <RelativeLayout 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"
         >
        <!-- Root element should wrap to parent size. -->
    
        <!-- Your view xml codes. -->
    
        <!--Bottom bar layout should be in root element. Parent should be Relative layout so that we can always align to parent bottom-->
        <LinearLayout  
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:weightSum="4">"
            <Button android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:text="25%"
            android:layout_weight="1"/>
            <Button android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:text="50%"
            android:layout_weight="2"/>
            <Button android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:text="25%"
            android:layout_weight="1"/>
        </LinearLayout>
    
    </RelativeLayout>
    

    希望这能解决您的问题。

    【讨论】:

    • 大家感谢大家的回答。我能够实现我想要的,但问题是,页脚没有固定在底部,它在中间。
    猜你喜欢
    • 1970-01-01
    • 2012-02-28
    • 2011-12-11
    • 2014-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-25
    • 2015-09-18
    相关资源
    最近更新 更多