如何使Android的一个布局中的看法? [英] How does Android render Views within a Layout?

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

问题描述

我有一个(我认为)简单的LinearLayout麻烦。 (长的问题 - 对不起)我有一个RelativeLayout的一个XML布局如下:

I am having trouble with a (I think) simple LinearLayout. (Long question - sorry) I have an XML layout with a RelativeLayout as follows:

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

    <TextView
        android:id="@+id/display_Player_Init"
        android:layout_width="40dp"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:text="Player" />

    <TextView
        android:id="@+id/display_USGA_Index"
        android:layout_width="40dp"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="5dp"
        android:gravity="right"
        android:layout_toRightOf="@+id/display_Tees"
        android:text="IDX" />

    <TextView
        android:id="@+id/display_Course_HCP"
        android:layout_width="40dp"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="5dp"
        android:gravity="right"
        android:layout_toRightOf="@+id/display_USGA_Index"
        android:text="HCP" />

    <TextView
        android:id="@+id/display_Course"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="5dp"
        android:layout_toRightOf="@+id/display_Player_Init"
        android:text="Course" />

    <TextView
        android:id="@+id/display_Tees"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="5dp"
        android:layout_toRightOf="@+id/display_Course"
        android:text="Tees" />

</RelativeLayout>

这显示了IDE preVIEW正确如下:

This shows in the IDE preview correctly as follows:

废话,没有足够的代表处点张贴的图像。

Crap, not enough rep points to post an image.

和应用程序正确显示这(注5值的顺序):

And the app correctly displays this (note the order of the 5 values):

没有足够的代表处点张贴的图像。

Not enough rep points to post an image.

在RelativeLayout的似乎过于笨重,我想只是使用更简单的LinearLayout如下:

The RelativeLayout seemed overly bulky and I wanted to just use a simpler LinearLayout as follows:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    >

    <TextView
        android:id="@+id/display_Player_Init"
        android:layout_width="40dp"
        android:layout_height="wrap_content"
        android:text="Player" />

    <TextView
        android:id="@+id/display_USGA_Index"
        android:layout_width="40dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:gravity="right"
        android:text="IDX" />

    <TextView
        android:id="@+id/display_Course_HCP"
        android:layout_width="40dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:gravity="right"
        android:text="HCP" />

    <TextView
        android:id="@+id/display_Course"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:text="Course" />

    <TextView
        android:id="@+id/display_Tees"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:text="Tees" />

</LinearLayout>

其中如下(注意字段是出从第一布局顺序,我要纠正。)正确previewed在IDE:

Which previewed correctly in IDE as follows (note the fields are out of order from the first layout. I am about to correct):

没有足够的代表处点张贴的图像。

Not enough rep points to post an image.

和正确显示,在应用程序如下:

And renders properly in the app as follows:

没有足够的代表处点张贴的图像。

Not enough rep points to post an image.

现在,当我更改的LinearLayout XML的TextViews的顺序,它呈现仿佛错误的值在田里显示。下面是XML:

Now, when I change the order of the TextViews in the LinearLayout XML, it renders as if the wrong values are showing in the fields. Here is the XML:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    >

    <TextView
        android:id="@+id/display_Player_Init"
        android:layout_width="40dp"
        android:layout_height="wrap_content"
        android:text="Player" />

    <TextView
        android:id="@+id/display_Course"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:text="Course" />

    <TextView
        android:id="@+id/display_Tees"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:text="Tees" />

    <TextView
        android:id="@+id/display_USGA_Index"
        android:layout_width="40dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:gravity="right"
        android:text="IDX" />

    <TextView
        android:id="@+id/display_Course_HCP"
        android:layout_width="40dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:gravity="right"
        android:text="HCP" />

</LinearLayout>

下面是(正确)的IDE preVIEW:
没有足够的代表处点张贴的图像。

Here is the (correct) IDE preview: Not enough rep points to post an image.

但是,在这里是应用程序的渲染:

But, here is the app rendering:

没有足够的代表处点张贴的图像。
它把字符串ABC CC在右列和包装它,就好像它是把在外地HCP字符串。

请注意字段顺序是正确的,但值被莫名其妙地切换。该XML领域呈现以正确的顺序,但字段的值是:名称,指数,T恤,HCP,课程以课程wrpped到HCP场的宽度值

Note the field order is correct but the values are somehow switched. The XML fields are rendered in the proper order, but the values of the fields are: Name, Index, Tees, HCP, Course with the value of "course" wrpped to the width of the HCP field.

没有code在这些例子之间变化,所以会出现该XML是造成问题。谁能帮我明白为什么? (很抱歉没有图像。)

No code is changed at between these examples, so it appears that the xml is causing the problems. Can anyone help me understand why? (Sorry about no images.)

感谢。

推荐答案

在分配资源ID到UI元素,如使用 Android的一个TextView:ID = , int数据字段在R.java文件中创建。

When you assign a resource id to a UI element such as a TextView using android:id=, an int data field is created in the R.java file.

在R.java文件可以在您的项目/根目录中找到,并与该目录中的所有文件,它是自动生成在构建时。

The R.java file can be found in the /gen directory in your project and, as with all files in this directory, it is auto-generated at build time.

有时,如果你重新工作,你的资源文件(如布局XML文件),其中有previously被分配到一个资源(如一个TextView)一个int值可以指不同的资源,即,另一TextView的。这意味着当您使用类似 findViewById(...),即使R.id指的是你想要的UI元素,实际INT资源ID所指向不同的东西。

Sometimes if you re-work your resource files (such as layout XML files) an int value which had previously been assigned to one resource (such as a TextView) may refer to a different resource, i.e., another TextView. This means when you use something like findViewById(...), even though the R.id is referring to the UI element you want, the actual int resource id is pointing at something different.

要解决这类问题的最简单方法是清洁项目 - 这将删除所有自动生成的文件,并重新创建它们并重新生成项目。

The easiest way to fix this sort of problem is to Clean the project - this deletes any auto-generated files and re-creates them and re-builds the project.

这篇关于如何使Android的一个布局中的看法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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