Android 安卓布局中心和底部
在我的布局中,我想在中间显示两个ImageButton,在底部显示一个my amountmeter,全部垂直居中。我尝试了很多方法,但似乎找不到完美的布局。所有的组件都垂直和居中对齐,但它使我的amountmeter变小,而不是屏幕的全宽 应该怎样 现在怎么样 更新代码 当前布局:Android 安卓布局中心和底部,android,Android,在我的布局中,我想在中间显示两个ImageButton,在底部显示一个my amountmeter,全部垂直居中。我尝试了很多方法,但似乎找不到完美的布局。所有的组件都垂直和居中对齐,但它使我的amountmeter变小,而不是屏幕的全宽 应该怎样 现在怎么样 更新代码 当前布局: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_par
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical" >
<ImageButton
android:id="@+id/purchase"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@drawable/round_button"
android:src="@drawable/ic_cart" />
<ImageButton
android:id="@+id/sell"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@drawable/round_button"
android:src="@drawable/ic_sell" />
<com.example.mobile.view.AmountMeterView
android:id="@+id/mobileAmount"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center|bottom"
android:padding="4dp" />
一个线性布局就足够了。不要将您的
AmountMeterView
放入它自己的线性布局中
只需做一个线性布局(垂直),并把所有三个元素都放在那里。所有元素的重心都是水平的。试着用这种方法,希望能对你有所帮助
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >
<ImageButton
android:id="@+id/purchase"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@drawable/round_button"
android:src="@drawable/ic_launcher" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >
<ImageButton
android:id="@+id/sell"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@drawable/round_button"
android:src="@drawable/ic_launcher" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="2"
android:orientation="vertical" >
<com.example.mobile.view.AmountMeterView
android:id="@+id/mobileAmount"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="4dp" />
</LinearLayout>
</LinearLayout>
复制代码下方的粘贴
<?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"
android:orientation="horizontal" >
<ImageButton
android:id="@+id/purchase"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="@drawable/ic_launcher" />
<ImageButton
android:id="@+id/sell"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/purchase"
android:layout_centerHorizontal="true"
android:src="@drawable/ic_launcher" />
<com.example.mobile.view.AmountMeterView
android:id="@+id/mobileAmount"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/sell"
android:padding="4dp"
android:src="@drawable/ic_launcher" />
</RelativeLayout>
把这行放在你的两个图像按钮上当然对你有用,如果你需要更多帮助,尽管问吧。更改了它,仍然显示所有对齐的内容等100%,但我的amountmeter仍然被裁剪,没有显示屏幕的全宽,即使布局宽度设置为填充父项。嗨,谢谢你的回复,使用这段代码,我得到了以下结果:好的,给你的android:layout\u weight=“1”,如果在amountmeter中添加一个权重为1,则布局不变。我得到了一个解决方案,你必须在AmountMeterVie中设置android:gravity=“center”。谢谢你的回复,效果很好,amountmeter仍然没有显示屏幕的全宽,但它很好,我只是想保持你提到的方式,谢谢你的回答,如果可以的话,最后一个问题是,如何调整与底部对齐的组件的大小?我的观点有问题。可以使用dip设置宽度吗?它对不同的屏幕大小会有怎样的反应?正常设置静态值的宽度是不好的编程,因为在android中有不同的屏幕大小,并且要提供多屏幕支持,这样的问题就会出现,所以通过重力和重量来提供高度和宽度是很好的
android:background="@drawable/round_button"
android:src="@drawable/ic_sell"
android:background="@drawable/round_button"
android:src="@drawable/ic_cart"