伙计们,我需要用三个按钮制作一个页脚,这样左右按钮应该从左右占据屏幕的 25%,而居中的图像应该占据屏幕的 50%,并且它们之间不留任何空间.
如果有人可以提供帮助,那就太好了。
我想发布所需页脚的图像,但我不允许共享图像。
伙计们,我需要用三个按钮制作一个页脚,这样左右按钮应该从左右占据屏幕的 25%,而居中的图像应该占据屏幕的 50%,并且它们之间不留任何空间.
如果有人可以提供帮助,那就太好了。
我想发布所需页脚的图像,但我不允许共享图像。
这里是:
<?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>
这是它的外观:
如果您想在所有活动中自定义底栏,那么您需要在所有活动中包含布局。我认为下面的代码可能会对您有所帮助:
在布局目录中创建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>
在底部的所有活动中包含上述 xml,例如:
< LinearLayout android:id="@+id/bottombar" android:layout_width="wrap_content" android:layout_height="50dp" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:背景="@android:color:black" android:gravity="center_horizontal" >
在 .xml 布局文件中包含 bottonlayout 的所有 .Java 文件中编写下面的代码。
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....
}
});
编写布局 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>
希望这能解决您的问题。