Android Studio手机布局

Android Studio手机布局,android,android-layout,Android,Android Layout,几乎疯狂地为手机创建布局XML。也许我使用Xcode的时间太长了,或者根本不知道如何在Android上正确地使用Xcode 我曾经尝试过创建大小(小、普通、大、xlarge)和密度的布局,但没有任何效果 我有我的屏幕背景,附加,并喜欢在布局上定位两个输入字段和按钮。使用相对布局,效果很好。但是,当在设备上运行时,无论是真实设备还是模拟器,这些字段永远不会位于背景之上。我做错了什么 用于常规布局的XML: <?xml version="1.0" encoding="utf-8"?> &

几乎疯狂地为手机创建布局XML。也许我使用Xcode的时间太长了,或者根本不知道如何在Android上正确地使用Xcode

我曾经尝试过创建大小(小、普通、大、xlarge)和密度的布局,但没有任何效果

我有我的屏幕背景,附加,并喜欢在布局上定位两个输入字段和按钮。使用相对布局,效果很好。但是,当在设备上运行时,无论是真实设备还是模拟器,这些字段永远不会位于背景之上。我做错了什么

用于常规布局的XML:

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

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/imageView3"
        android:src="@drawable/login2"
        android:scaleType="fitXY" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:id="@+id/tfPass"
        android:hint="CONTRASEÑA"
        android:inputType="textPassword"
        android:textColor="#000000"
        android:background="#00000000"
        android:singleLine="true"
        android:layout_weight="1"
        android:layout_x="3dp"
        android:layout_y="342dp"
        android:layout_alignTop="@+id/tfMail"
        android:layout_marginTop="76dp"
        android:layout_alignLeft="@+id/tfMail"
        android:layout_alignStart="@+id/tfMail"
        android:layout_alignRight="@+id/tfMail"
        android:layout_alignEnd="@+id/tfMail" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:id="@+id/tfMail"
        android:hint="EMAIL large"
        android:textColor="#000000"
        android:background="#00000000"
        android:singleLine="true"
        android:inputType="textEmailAddress"
        android:layout_weight="1"
        android:layout_x="3dp"
        android:layout_y="287dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginBottom="353dp"
        android:layout_marginLeft="60dp"
        android:layout_marginRight="60dp" />

    <ImageButton
        android:layout_width="250dp"
        android:layout_height="50dp"
        android:id="@+id/btnLogin"
        android:background="#00000000"
        android:onClick="sel_Login2_login"
        android:layout_weight="1"
        android:layout_x="2dp"
        android:layout_y="413dp"
        android:layout_below="@+id/tfPass"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="48dp" />

</RelativeLayout>

我在没有动作栏的情况下全屏预览。我在该活动的清单中也有:

   <activity
        android:name=".Login2View"
        android:label="@string/app_name" android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
        android:configChanges="orientation"
        android:screenOrientation="portrait" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

更新-完整XML布局

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

    <EditText
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:id="@+id/tfPass"
        android:hint="CONTRASEÑA"
        android:inputType="textPassword"
        android:textColor="#000000"
        android:background="#00000000"
        android:singleLine="true"
        android:layout_weight="1"
        android:layout_x="3dp"
        android:layout_y="342dp"
        android:layout_alignTop="@+id/tfMail"
        android:layout_marginTop="55dp"
        android:layout_alignLeft="@+id/tfMail"
        android:layout_alignStart="@+id/tfMail"
        android:layout_alignRight="@+id/tfMail"
        android:layout_alignEnd="@+id/tfMail" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:id="@+id/tfMail"
        android:hint="EMAIL normal"
        android:textColor="#000000"
        android:background="#00000000"
        android:singleLine="true"
        android:inputType="textEmailAddress"
        android:layout_weight="1"
        android:layout_x="3dp"
        android:layout_y="287dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginBottom="255dp"
        android:layout_marginLeft="50dp"
        android:layout_marginRight="50dp" />

    <ImageButton
        android:layout_width="215dp"
        android:layout_height="50dp"
        android:id="@+id/btnLogin"
        android:background="#00000000"
        android:onClick="sel_Login2_login"
        android:layout_weight="1"
        android:layout_x="2dp"
        android:layout_y="413dp"
        android:layout_below="@+id/tfPass"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp" />

</RelativeLayout>

更新日期:2015年11月3日

我将附加一个来自Android Studio的屏幕截图。在不同设备上使用相同的XML布局时,EditText和按钮放错了位置。我已经为每一个设置了不同的背景色。这是Android Studio中显示的结果,在设备上看起来是一样的:


您必须在顶部父视图中添加背景

例如,如果父视图是
相对视图,则必须执行以下操作:

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

<EditText
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:id="@+id/tfPass"
    android:hint="CONTRASEÑA"
    android:inputType="textPassword"
    android:textColor="#000000"
    android:background="#00000000"
    android:singleLine="true"
    android:layout_weight="1"
    android:layout_x="3dp"
    android:layout_y="342dp"
    android:layout_alignTop="@+id/tfMail"
    android:layout_marginTop="76dp"
    android:layout_alignLeft="@+id/tfMail"
    android:layout_alignStart="@+id/tfMail"
    android:layout_alignRight="@+id/tfMail"
    android:layout_alignEnd="@+id/tfMail" />

<EditText
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:id="@+id/tfMail"
    android:hint="EMAIL large"
    android:textColor="#000000"
    android:background="#00000000"
    android:singleLine="true"
    android:inputType="textEmailAddress"
    android:layout_weight="1"
    android:layout_x="3dp"
    android:layout_y="287dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginBottom="353dp"
    android:layout_marginLeft="60dp"
    android:layout_marginRight="60dp" />

<ImageButton
    android:layout_width="250dp"
    android:layout_height="50dp"
    android:id="@+id/btnLogin"
    android:background="#00000000"
    android:onClick="sel_Login2_login"
    android:layout_weight="1"
    android:layout_x="2dp"
    android:layout_y="413dp"
    android:layout_below="@+id/tfPass"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="48dp" />

</RelativeLayout>

你的背景是什么?带有@drawable/login2的“imageView3”?是的,这是正确的,为什么不将背景应用于根布局您应该使用dimens“@dimens/fourtyeight”而不是dimens“48dp”,因为您需要多分辨率支持谢谢。做了,但效果还是一样。在Nexus5上看起来像是设计好的,但在Galaxy S4上,使用与Nexus5相同的布局(正常),编辑文本向下移动约15px。使用全屏和NoActionBar,这可能会影响布局吗?@DietmarSchwarzWebers,请共享您的完整布局Laurenty,我已将完整布局XML添加为我的帖子的更新。您需要根据设备分辨率指定不同文件夹中的边距。在XML静态边距中,接缝可能是罪魁祸首。这里有一些关于一角硬币的东西:-@LaurentY@DietmarSchwarzWebers,您的完整布局似乎还可以,这个布局的结果如何?获得的结果和预期的结果有什么区别?
RelativeLayout headerLayout = (RelativeLayout)findviewbyid(R.id.headerLayout);
EditText tfPass = new EditText(this);
ViewTreeObserver observer = headerLayout.getViewTreeObserver();
observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            int width  = headerLayout.getMeasuredWidth();
            int height = headerLayout.getMeasuredHeight();
            headerLayout .getViewTreeObserver().removeGlobalOnLayoutListener(
                this);

            //add + places components
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(WITH_CALCULATED_WITH_PERCENTAGES, HEIGHT_WITH_CALCULATED_WITH_PERCENTAGES);
            params.leftMargin = (int)(width*12.5);
            params.topMargin = (int)(height*32.8);
            headerLayout.addView(tfPass, params);
        }
});