简体   繁体   English

在布局底部创建4个ImageButton

[英]Create 4 ImageButtons at bottom of the layout

I am trying to display 4 ImageButtons at the bottom of the layout. 我正在尝试在布局的底部显示4个ImageButton。 I am able to get only 3 ImageButtons. 我只能得到3个ImageButton。 The fourth ImageButton is not visible. 第四个ImageButton不可见。 and here is my code for that. 这是我的代码。

I am using Relative Layout for to display the application. 我正在使用相对布局来显示应用程序。

<ImageButton
    android:id="@+id/Button1"
    android:layout_weight="1.0"
    android:layout_gravity="bottom"
android:layout_alignParentBottom="true"
    android:longClickable="true"
    android:layout_width="wrap_content"
    android:layout_height="75sp"
    android:background="@android:color/transparent"
    android:src="@drawable/imagebutton2"/>

 <ImageButton
    android:id="@+id/Button2"
    android:layout_gravity="bottom"
    android:layout_toRightOf="@+id/Button1"
    android:layout_width="wrap_content"
    android:layout_height="75sp"
    android:background="@android:color/transparent"
    android:src="@drawable/imagebutton1"
    android:layout_weight="1.0"
    android:layout_marginLeft="2dp"
    android:longClickable="true"/>

 <ImageButton
     android:id="@+id/Button3"
     android:layout_gravity="bottom" 
     android:layout_toRightOf="@+id/Button2"
     android:layout_height="75sp"
     android:layout_width="wrap_content"
     android:layout_weight="1.0"
     android:background="@android:color/transparent"
     android:src="@drawable/imagebutton1"
     android:layout_marginLeft="2dp"
     android:longClickable="true"/>
 <ImageButton
     android:id="@+id/Button4"
     android:layout_gravity="bottom" 
     android:layout_height="75sp"
     android:layout_width="wrap_content"
     android:layout_weight="1.0"
     android:background="@android:color/transparent"
     android:src="@drawable/imagebutton1"
     android:layout_marginLeft="2dp"
     android:longClickable="true"
     android:layout_alignParentRight="true"/>

Put it in a LinearLayout with weights and align this LinearLayout tot he bottom of the parent like this: 将其放入带权重的LinearLayout ,并将此LinearLayout与父对象的底部对齐,如下所示:

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

    <ImageButton
        android:id="@+id/ib1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

    <ImageButton
        android:id="@+id/ib2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

    <ImageButton
        android:id="@+id/ib3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

    <ImageButton
        android:id="@+id/ib4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

</LinearLayout>

Note that this method will decrease the performance somewhat. 请注意,此方法会降低性能。

First you need to remove this from your ImageButton attributes if you want to keep using RelativeLayout as their parent layout: 首先,如果要继续使用RelativeLayout作为其父级布局,则需要将其从ImageButton属性中删除:

android:layout_weight="1.0"

It is used in LinearLayout, Lint should be giving you a warning about it (invalid layout param in RelativeLayout). 它在LinearLayout中使用,Lint应该向您发出警告(RelativeLayout中的无效布局参数)。

If you want your 4 buttons to show in the bottom of the screen you need to include 如果要在屏幕底部显示4个按钮,则需要添加

android:layout_alignParentBottom="true" 

in all 4 ImageButtons, I tried the xml you provide and only the 1st button is showing in the bottom. 在所有4个ImageButton中,我都尝试了您提供的xml,但只有第一个按钮显示在底部。

And last but not least, if you want your button to have the same size to have some design consistency, I would suggest putting them in a horizontal LinearLayout with 最后但并非最不重要的一点是,如果您希望按钮具有相同的大小以实现某些设计一致性,建议您将它们放置在具有以下内容的水平LinearLayout中:

android:layout_alignParentBottom="true"
android:layout_width="fill_parent"

and configure the ImageButtons with 并配置ImageButtons与

android:layout_width="0dp"
android:layout_weight="1.0" 

then include that LinearLayout in your RelativeLayout. 然后在您的RelativeLayout中包含该LinearLayout。

Another thing : since you're using 另一件事:由于您正在使用

android:layout_width="wrap_content"

for your ImageButtons, you need to make sure the image is not too wide, otherwise some of your buttons might not be shown on screen because the first one(s) is taking too much space, leaving the last one(s) to the right of your screen. 对于您的ImageButton,您需要确保图像不是太宽,否则某些按钮可能不会显示在屏幕上,因为第一个按钮占用了太多空间,而最后一个按钮则留在了右边屏幕上的

On a side note, I hope you're not trying to make a iOS-style lower tab bar, this is frowned upon in Android, more info here ... 附带一提,我希望您不要尝试制作iOS风格的下标签栏,这在Android中是不受欢迎的,更多信息在这里 ...

Have a good one ! 祝你有个好的一天 !

At the last ImageButton you don't have: 在最后一个ImageButton中,您没有:

android:layout_toRightOf="@+id/Button3"

You will need this if you want it to be at the bottom. 如果您希望它位于底部,则将需要它。

I would also suggest that you remove some of your code: 我还建议您删除一些代码:

android:layout_gravity="bottom" 
android:layout_weight="1.0"

This only works in FrameLayout or LinearLayout . 这仅适用于FrameLayoutLinearLayout

If you want to know for sure every ImageButton is at the bottom of the screen use what you used for the first button: 如果您想确定每个ImageButton都位于屏幕底部,请使用第一个按钮使用的按钮:

android:layout_alignParentBottom="true"

Like some people say, in the last button, you don't have android:layout_toRightOf = "@id/Button3" so it's going to be in the top of the layout. 就像有些人说的那样,在最后一个按钮中,您没有android:layout_toRightOf = "@id/Button3"因此它将位于布局的顶部。

Other way to do this that I usually do is: 我通常这样做的另一种方法是:

android:layout_toRightOf = "@id/Button1"
android:layout_alignbottom = @id/Button1"

It's going to align with the bottom of the button1. 它将与button1的底部对齐。 I do this because sometimes this button isn't align with the other one, depends of the layout. 我这样做是因为有时此按钮与另一个按钮不一致,具体取决于布局。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM