2013-04-18 118 views
0

我对布局有困难。布局:拉伸的背景图片

作为我的第一个开发项目,我试图制作一个简单的天文台。 我想要在背景中显示一个天文台的图片,并且在应用程序的底部有开始和停止按钮 ,并排并填充屏幕宽度。

这是我的最佳拍摄,但我不满意。

所有小部件都放置正确,但... 我的背景被拉伸,我的按钮似乎垂直压缩,甚至底部的文本有点裁剪。

我发现,如果我改变这些线

android:layout_width="fill_parent" 
android:layout_height="fill_parent" 

通过

android:layout_width="wrap_content" 
android:layout_height="wrap_content" 

背景是OK,则按钮太..但所有部件被放置在活动的中心。

我该如何解决这个问题?

顺便说一句,如果我想让我的按钮并排,我选择了更好的解决方案吗?

感谢您的帮助!查尔斯。

...现在我的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的。 我不知道如何在不使用填充的情况下使用两个相同大小的按钮,如果您希望您的应用在不同的屏幕上运行,我不认为这是个好主意。

无论如何,我想出了这个,但我仍然有我的拉伸图像和我的按钮不具有相同的大小。

<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

1)最有可能的是,你需要比'LinearLayout'更先进的东西; (2)你的“Chronometer”组件是什么? –

+0

Chronmeter是一个Android小部件 android.widget.Chronometer –

回答

0

我终于找到了解决方案。

第一个问题:我的9补丁图像隐藏了我的其他组件。

解决方法:在你的LinearLayout填充=“0dip” ...这里给了我我的回答(Android layout broken with 9-patch background

问题二的主题:加机器人为什么我的照片是streched而我设计的屏幕的确切大小。这是一个愚蠢的问题......这是因为操作栏和上面的其他栏(有电池电量和其他东西)。

要摆脱这些栏(包含在主题中),请在清单中使用不同版本的主题。

我选择:Theme.Light.NoTitleBar.Fullscreen。

已解决的问题。

0

尝试的RelativeLayout;)我认为这是很容易处理比LinearLayout中。也许你可以上传一张图片,看看它的样子,我会给你一个相关的布局解决方案;)

+0

我对这层有一些困难,我发现如何将这些按钮并排放置,而不是如何填充屏幕的所有宽度。 我附上了我的屏幕快照。 –

+0

[这里是快照](http://imageshack.us/photo/my-images/199/snapshotfv.png) –

+0

我编辑了我的消息与另一个代码示例。 –

0

你可以试试这个吗?应该工作IMO。 这是一种更好的/防呆的方式来获得您的布局。

<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" 
android:background="@drawable/background" 
tools:context=".Chronosv1" > 


<LinearLayout 
android:id="@+id/layout1" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:orientation="horizontal" 
android:weightSum="2" 
android:layout_alignParentBottom="true"> 
<Button 
android:id="@+id/button1" 
android:layout_width="0dp" 
android:layout_height="wrap_content" 
android:layout_weight="1" 
android:layout_margin="5dp" 
android:text="Start" /> 

<Button 
android:id="@+id/button2" 
android:layout_width="0dp" 
android:layout_height="wrap_content" 
android:layout_weight="1" 
android:layout_margin="5dp" 
android:text="Stop" /> 
</LinearLayout> 
<Chronometer 
android:id="@+id/chronometer1" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:layout_above="@+id/layout1" /> 
</RelativeLayout> 
+0

对于按钮没关系,但背景仍然是步进的。 –

+0

我注意到最上面的标题栏(带有应用程序名称的那个)对背景图片有影响......我不明白为什么,但是我的320x240图片由于它而不能正确显示。 我试图将我的图片转换为9补丁图像。现在在Eclipse/Graphical Layout中一切正常,但在我的AVD和手机上,其他组件不再显示出来了...... grrrrrr。 –