在R.id没有RelativeLayout的ID,同时获得RelativeLayout的尺寸 [英] No RelativeLayout id in R.id while Getting dimensions of RelativeLayout

查看:141
本文介绍了在R.id没有RelativeLayout的ID,同时获得RelativeLayout的尺寸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得的尺寸我的 RelativeLayout的

我许多告知使用下面的codeS:

I am told by many to use the following codes:

    RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.myRelativeLayout);
    int layoutWidth = relativeLayout.getWidth();
    int layoutHeight = relativeLayout.getHeight();

我的问题是,所有的IDS,我可以在我的 R.id找到都是按钮,文本视图和所有的那种。有没有像在所有布局ID!

My problem is that all the ids I can find in my R.id are all buttons, text views, and all that kind. There is nothing like layout id at all!

然后我试着写:

relativeLayout = (RelativeLayout) findViewById(R.layout.main_activity); 

RelativeLayout的为空时运行。因此,它也是不正确的。

relativeLayout is null when run. So it is also incorrect.

我需要手动添加布局ID为 R.id ?我不这么认为。

Do I need to add the layout id manually into R.id? I don't think so.

但为什么会出现在 R.id

推荐答案

这取决于你是什么意思布局。默认情况下,当你创建一个活动或XML布局,它会被存储为R.layout.xxx。但可以说,布局里面,你有另一种布局(在这种情况下,嵌套布局),您必须手动身份证,你要引用的布局。

That depends on what layout do you mean. By default, when you create an activity or a XML layout, it will be stored as R.layout.xxx . But lets say, inside a layout, you have another layout (nested layout in this case), you have to manually "id" the layout that you want to refer to.

例如:

<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"
    tools:context=".MainActivity" >

    <RelativeLayout
        android:id="@+id/CallMe"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="139dp"
        android:layout_marginTop="130dp" >
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="@+layout/test"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/CallMe"
        android:layout_below="@+id/CallMe"
        android:layout_marginTop="64dp" >
    </RelativeLayout>

</RelativeLayout>

然后,您可以参考由R.id.C​​allMe第一相对布局和R.layout.test第二个

You can then refer to the first relative layout by R.id.CallMe and the second one by R.layout.test

您可以使用任何名字,它并不必须是布局或ID。
希望帮助

You can name it anything, it doesn't has to be layout or id. Hope that helps

这篇关于在R.id没有RelativeLayout的ID,同时获得RelativeLayout的尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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