Android中的布局问题 [英] Layout Problem in Android

查看:99
本文介绍了Android中的布局问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对android来说还很陌生,并且在布局方面遇到了一些问题.下面是我想要的布局近似图. (我希望列表首先位于屏幕下方,以便用户可以向下滚动以查看它.)但是,目前的代码使左侧的所有视图仅占据屏幕的四分之一而不是一半,如图所示,即使我将所有宽度都设置为fill_parent.

I am fairly new to android and am having some problems with a layout. Below is an approximation of what I want the layout to look like. (I would like the list to be below the screen at first so that the user can scroll down to see it.) However my code as it currently stands makes all of the views on the left only take up a quarter the screen instead of half, as depicted, even though I have all widths set to fill_parent.

此外,此布局似乎使我的自定义视图上的坐标系混乱.通常,这不是什么大问题,但是在这种情况下,我使用该视图绘制图片,而该图片最终放置在错误的位置.是否有人知道如何解决这些问题?

Also, this layout seems to mess up the coordinate system on my custom view. Normally this wouldn't be much of an issue, but in this case I am using that view to draw a picture and the picture ends up in the wrong place. Does anybody have any idea how to resolve these issues?

这是我的代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical">
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical">
<TableRow>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent" android:orientation="vertical"
android:layout_width="fill_parent">
<TableRow>
 <TextView android:text="Resultant Force" android:id="@+id/resForceTitle"
  android:textSize="25dip" android:layout_width="fill_parent"
  android:layout_height="fill_parent" android:padding="3dip"
  android:gravity="center" android:layout_span="2" />
</TableRow>
<TableRow>
 <TextView android:id="@+id/magText" android:text="Magnitude (N) ="
  android:textSize="15dip" android:layout_width="fill_parent"
  android:layout_height="fill_parent" android:padding="3dip"
  android:gravity="center_vertical" android:layout_span="2" />
</TableRow>
<TableRow>
 <EditText android:id="@+id/magnitude" android:inputType="numberDecimal"
  android:layout_width="fill_parent" android:padding="3dip"
  android:layout_height="fill_parent" android:layout_span="2" />
</TableRow>
<TableRow>
 <TextView android:id="@+id/dirText" android:text="Angle (deg) ="
  android:layout_width="fill_parent" android:layout_height="fill_parent"
  android:textSize="15dip" android:gravity="center_vertical"
  android:layout_span="2" />
</TableRow>
<TableRow>
 <EditText android:id="@+id/direction" android:inputType="numberDecimal"
  android:layout_height="fill_parent" android:padding="3dip"
  android:layout_width="fill_parent" android:layout_span="2" />
</TableRow>
<TableRow>
 <Button android:id="@+id/pushButton" android:text="Add Force"
  android:layout_height="fill_parent" android:padding="3dip"
  android:layout_width="fill_parent" />
 <Button android:id="@+id/clearButton" android:text="Clear"
  android:layout_height="fill_parent" android:padding="3dip"
  android:layout_width="fill_parent" />
</TableRow>
</TableLayout>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<android.physicsengine.AxisDrawing
 android:id="@+id/image" android:layout_width="fill_parent"
 android:layout_height="fill_parent" />
</FrameLayout>
</TableRow>
</TableLayout>
<ListView android:id="@android:id/list" android:layout_height="wrap_content"
android:padding="3dip" android:layout_width="fill_parent"
android:clickable="false" />
<TextView android:id="@android:id/empty" android:text="Add Forces to List Components"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:textSize="15dip" android:gravity="center_vertical" />
</LinearLayout>

推荐答案

从XML的外观来看,您似乎误解了fill_parent的含义.意思是和我父母一样大".另外,为什么要使用两个嵌套的TableLayouts?要将屏幕平均分为两部分,您应该使用水平的LinearLayout并为每个子项(TableLayout和您的自定义视图)赋予0dip的宽度,并为1:1的layout_weight

From the look of your XML it looks like you misunderstand what fill_parent means. It means "be as big as my parent." Also, why are you using two nested TableLayouts? To divide the screen evenly in two you should use a horizontal LinearLayout and give each child (the TableLayout and your custom View) a width of 0dip and a layout_weight of 1:

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <TableLayout
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_height="wrap_content">
            ...
    </TableLayout>
    <android.physicsengine.AxisDrawing
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_height="wrap_content" />
</LinearLayout>

这篇关于Android中的布局问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆