Android小白快速编写APP登录界面
(本篇文章在原文章上做了一定的修改,若参考原文请拉到最后)
首先看一下效果图
1、打开Android Studio
2、选择空白界面
3、activity_main文件代码
这个界面主要是上方的APP标志位置,若不需要可以选择删除
<?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="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="false"
android:drawableTop="@drawable/zhuozhuo"
android:gravity="center"
android:text="@string/logo" />
</LinearLayout>
</RelativeLayout>
4、在res>>layout文件中新建XML命名为anctivity_login
若不需要上一个标志界面,此代码可直接放在activity_main中
5、在activity_login文件中编写界面代码
drawable为引入的矢量图,插入矢量图只需要将下载的图片复制到res>>drawable目录下以相应的名字命名即可
(推荐矢量图下载地址:https://www.iconfont.cn/search/index?q)
<?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:background="#f0f0f0"
android:orientation="vertical" >
<include layout="@layout/activity_main" />//此句为包含上一界面,不需要则删除
<LinearLayout
android:layout_width="match_parent"
android:layout_height="55dp"
android:layout_marginTop="30dp"
android:background="#fff"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp"
android:text="@string/phonenumber"
android:textColor="#838383"
android:textSize="17sp" />
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:background="@android:color/transparent"
android:hint="@string/inputphonenumber"
android:maxLength="11"
android:inputType="phone"
android:textSize="17sp" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1px"
android:background="#f0f0f0" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="55dp"
android:background="#fff"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp"
android:text="@string/password"
android:textColor="#838383"
android:textSize="17sp" />
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:maxLength="16"
android:background="@android:color/transparent"
android:hint="@string/inputpassword"
android:inputType="textPassword"
android:textSize="17sp" />
</LinearLayout>
<Button
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="40dp"
android:background="@drawable/buttonsearch"
android:text="@string/login"
android:textSize="17sp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="30dp"
android:text="@string/getpasswordback"
android:textColor="#838383"
android:textSize="15sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="30dp"
android:text="@string/phonefastlogin"
android:textColor="#838383"
android:textSize="15sp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
<LinearLayout
android:id="@+id/id_fast_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginBottom="50dp"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:clickable="true"
android:drawableTop="@drawable/qq"
android:gravity="center"
android:text="@string/tencentQQ" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:clickable="true"
android:drawableTop="@drawable/wechat"
android:gravity="center"
android:text="@string/weixin" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:clickable="true"
android:drawableTop="@drawable/sina"
android:gravity="center"
android:text="@string/sina" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/id_fast_login"
android:layout_centerHorizontal="true"
android:layout_marginBottom="20dp"
android:layout_marginTop="20dp"
android:text="@string/fastlogin" />
</RelativeLayout>
</LinearLayout>
6、编写String。res>>values中新建string.xml文件
这里可以根据自己的需求插入文字等
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">灼灼</string>
<string name="logo">灼灼</string>
<string name="title">灼灼</string>
<string name="phonenumber">手机号码</string>
<string name="inputphonenumber">请输入手机号码</string>
<string name="password">密码</string>
<string name="inputpassword">输入密码</string>
<string name="getpasswordback">找回密码</string>
<string name="phonefastlogin">手机快速登录</string>
<string name="login">登录</string>
<string name="tencentQQ">qq</string>
<string name="weixin">微信</string>
<string name="sina">新浪</string>
<string name="fastlogin">--快捷登录--</string>
</resources>
7、在自动生成的MainActivity.java文件中改一下文件名
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
}
}
原文参考来自:
https://blog.csdn.net/qq_36232611/article/details/103929460
本文作为自我记录的一篇博客,如有描述不当或明显错误的地方,请各位大佬指导。