0

I have difficulties with my layout.

As my first dev project, I'm trying to make a simple chronometer. I want to have a picture of a chronometer in the background and the start and stop button at the bottom of the application, side by side and filling the width of the screen.

Here is my best shot, but I'm not satisfied.

All widgets are placed properly but ... My background is streched and my buttons seem vertically compressed, even the text at the bottom is a bit cropped.

I found that if I change these lines

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

by

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

The background is ok, the buttons too .. but all widgets are placed in the center of the Activity.

How can I fix this ?

By the way if I want to have my buttons side by side, did I choose the better solution ?

Thanks for your help ! Charles.

... and now my 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>

I tried relativeLayout. I don't know how to have two buttons of the same size without using a padding, I don't think it's a good idea if you want your app to run on different screens.

Anyway I come up with this, but I still have my streched image and my buttons don't have the same size.

<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" />

4

3 回答 3

0

你能试试这个吗?应该工作海事组织。这是获取布局的更好/防万一的方法。

<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> 
于 2013-04-19T04:25:17.090 回答
0

试试RelativeLayout ;) 我认为这比LinearLayout 更容易处理。也许你可以上传一张图片,我会给你一个相对布局的解决方案;)

于 2013-04-18T09:43:24.507 回答
0

我终于找到了解决方案。

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

解决方案:在您的线性布局中添加android:padding="0dip" ...这里是给我答案的主题(Android 布局被 9-patch 背景破坏

第二个问题:为什么我的图片被拉伸了,而我却按照屏幕的确切尺寸设计了它。这是一个愚蠢的问题......这是因为上面的操作栏和另一个栏(电池电量和其他东西)。

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

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

问题解决了。

于 2013-05-08T08:49:47.743 回答