【发布时间】:2023-12-03 05:53:01
【问题描述】:
我的布局有问题。
作为我的第一个开发项目,我正在尝试制作一个简单的计时器。 我想在背景中有一张计时器的照片以及开始和停止按钮 在应用程序的底部,并排并填充屏幕的宽度。
这是我最好的镜头,但我并不满意。
所有小部件都已正确放置,但... 我的背景被拉伸了,我的按钮看起来被垂直压缩了,甚至底部的文字也被剪掉了一点。
我发现如果我改变这些行
android:layout_width="fill_parent"
android:layout_height="fill_parent"
通过
android:layout_width="wrap_content"
android:layout_height="wrap_content"
背景还可以,按钮也可以..但是所有的小部件都放在了Activity的中心。
我该如何解决这个问题?
顺便说一句,如果我想让我的按钮并排,我是否选择了更好的解决方案?
感谢您的帮助! 查尔斯。
...现在是我的xml ....
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@drawable/background"
tools:context=".Chronosv1" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_weight="2" >
<Chronometer
android:id="@+id/chronometer1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="1" >
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="50"
android:text="START" />
<Button
android:id="@+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="50"
android:text="STOP" />
</LinearLayout>
我尝试了相对布局。 我不知道如何在不使用填充的情况下拥有两个大小相同的按钮,如果您希望应用在不同的屏幕上运行,我认为这不是一个好主意。
无论如何我想出了这个,但我仍然有我的拉伸图像,我的按钮没有相同的大小。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/background"
tools:context=".Chronosv1" >
<Chronometer
android:id="@+id/chronometer1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Start" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_toRightOf="@id/button1"
android:text="Stop" />
【问题讨论】:
-
1) 很可能,您需要比
LinearLayout更高级的东西; (2) 你的Chronometer组件是什么? -
Chronmeter 是一个 android 小部件 android.widget.Chronometer
标签: android android-layout android-widget