【问题标题】:Can't change with android XML file无法使用 android XML 文件进行更改
【发布时间】:2016-04-05 02:34:04
【问题描述】:

我想构建一个这样的 XML 文件:

但我不能。有人能告诉我为什么会这样吗?如何制作编辑框提示的中心?

特别编辑框

这是我的代码:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >

            <RelativeLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent">

                <Button
                    android:id="@+id/button1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="Button1"
                    android:layout_marginTop="67dp"
                    android:layout_below="@+id/linearLayout"
                    android:layout_centerHorizontal="true"
                    android:background="#013567" />

                <LinearLayout
                    xmlns:android="http://schemas.android.com/apk/res/android"
                    android:orientation="vertical"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:padding="10dip"
                    android:gravity="center"
                    android:layout_marginTop="51dp"
                    android:id="@+id/linearLayout"
                    android:weightSum="1"
                    android:layout_below="@+id/textView4"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true">
                    <!--  Email Label -->
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textAppearance="?android:attr/textAppearanceLarge"
                        android:text="Large Text"
                        android:id="@+id/textView"
                        android:layout_below="@+id/topBar"
                        android:layout_centerHorizontal="true"
                        android:textColor="#fe9900"
                        android:layout_weight="0.07"
                        android:layout_gravity="center_horizontal" />

                    <EditText android:layout_width="268dp"
                        android:layout_height="match_parent"
                        android:layout_marginTop="5dip"
                        android:layout_marginBottom="20dip"
                        android:singleLine="true"
                        android:background="#ffffff"
                        android:hint="Enter Email Address" />
                    <!--  Password Label -->
                    <EditText android:layout_width="274dp"
                        android:layout_height="fill_parent"
                        android:layout_marginTop="5dip"
                        android:singleLine="true"
                        android:password="true"
                        android:background="#ffffff"
                        android:hint="Password" />
                    <!-- Login button -->
                    <Button android:id="@+id/btnLogin"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="10dip"
                        android:text="Login"
                        android:background="#fe9900"
                        android:layout_gravity="center_horizontal" />

                    <!-- Link to Registration Screen -->

                </LinearLayout>

                <LinearLayout
                    android:orientation="vertical"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignParentTop="true"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true"
                    android:layout_marginTop="45dp"
                    android:id="@+id/linearLayout2">


                </LinearLayout>

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:text="Medium Text"
                    android:id="@+id/textView2"
                    android:layout_alignParentBottom="true"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true"
                    android:layout_marginLeft="41dp"
                    android:layout_marginStart="41dp"
                    android:layout_marginBottom="38dp" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:text="Medium Text"
                    android:id="@+id/textView3"
                    android:layout_alignBottom="@+id/textView2"
                    android:layout_toRightOf="@+id/textView2"
                    android:layout_toEndOf="@+id/textView2"
                    android:layout_marginLeft="31dp"
                    android:layout_marginStart="31dp" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:text="Medium Text"
                    android:id="@+id/textView4"
                    android:layout_alignTop="@+id/linearLayout2"
                    android:layout_toLeftOf="@+id/textView3"
                    android:layout_toStartOf="@+id/textView3" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:text="Medium Text"
                    android:id="@+id/textView5"
                    android:layout_alignTop="@+id/linearLayout2"
                    android:layout_alignLeft="@+id/textView3"
                    android:layout_alignStart="@+id/textView3"
                    android:layout_marginLeft="34dp"
                    android:layout_marginStart="34dp" />

            </RelativeLayout>

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="New Button"
            android:id="@+id/button"
            android:background="#013469" />



    </RelativeLayout>

</RelativeLayout>

【问题讨论】:

  • 我在这里看到了几个问题。 1 - fill_parent 已在 6 年前被弃用。请改用match_parent2 - 你使用了很多嵌套的布局,这些你真的不需要并且对性能有影响。如果使用正确,只需一个 RelativeLayout 即可。并且会表现得更好。

标签: android xml view


【解决方案1】:

在要居中的edittext xml 中添加android:gravity="center_horizontal",并从电子邮件文本视图中删除android:layout_centerHorizontal="true"。您还应该删除 android:password="true" 已弃用。只是一点提示,当一行 xml 代码为黄色时。它可能有问题,也可能有问题。将其悬停并查看一条消息,然后单击左侧的灯泡以获取更多选项。像下面这样的东西应该可以工作。

<!--  Email Label -->
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:text="Large Text"
                android:id="@+id/textView"
                android:layout_below="@+id/topBar"
                android:textColor="#fe9900"
                android:layout_weight="0.07"
                android:layout_gravity="center_horizontal" />

            <EditText android:layout_width="268dp"
                android:layout_height="match_parent"
                android:layout_marginTop="5dip"
                android:layout_marginBottom="20dip"
                android:singleLine="true"
                android:background="#ffffff"
                android:gravity="center_horizontal"
                android:hint="Enter Email Address" />
            <!--  Password Label -->
            <EditText android:layout_width="274dp"
                android:layout_height="fill_parent"
                android:layout_marginTop="5dip"
                android:singleLine="true"
                android:password="true"
                android:gravity="center_horizontal"
                android:background="#ffffff"
                android:hint="Password" />

【讨论】:

    猜你喜欢
    • 2015-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-09
    • 1970-01-01
    相关资源
    最近更新 更多