Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit260e9d2

Browse files
author
lanxiaobin
committed
UI 优化,xml转代码
1 parent265c803 commit260e9d2

File tree

6 files changed

+67
-13
lines changed

6 files changed

+67
-13
lines changed

‎app/build.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,8 @@ dependencies {
3434
androidTestImplementation'androidx.test.espresso:espresso-core:3.1.1'
3535

3636
implementation'androidx.multidex:multidex:2.0.0'
37+
38+
//编译期 将布局xml 转换成java代码
39+
annotationProcessor'com.zhangyue.we:x2c-apt:1.1.2'
40+
implementation'com.zhangyue.we:x2c-lib:1.0.6'
3741
}

‎app/src/main/java/com/lanshifu/launchtest/MainActivity.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,24 @@
44

55
importandroid.os.Bundle;
66

7+
importcom.zhangyue.we.x2c.X2C;
8+
importcom.zhangyue.we.x2c.ano.Xml;
9+
10+
11+
/**
12+
* UI 优化,xml 编译期转 java代码
13+
*/
14+
@Xml(layouts ="activity_main")
715
publicclassMainActivityextendsAppCompatActivity {
816

917
@Override
1018
protectedvoidonCreate(BundlesavedInstanceState) {
1119
setTheme(R.style.AppTheme);
1220
super.onCreate(savedInstanceState);
13-
setContentView(R.layout.activity_main);
21+
// setContentView(R.layout.activity_main);
1422

1523
setTitle("主页");
24+
X2C.setContentView(this,R.layout.activity_main);
25+
1626
}
1727
}

‎app/src/main/java/com/lanshifu/launchtest/MyApplication.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,14 @@ private void loadMultiDex(Context context) {
6565
longstartTime =System.currentTimeMillis();
6666
MultiDex.install(context);
6767
Log.d(TAG,"第二次 MultiDex.install 结束,耗时: " + (System.currentTimeMillis() -startTime));
68+
69+
preNewActivity();
70+
}
71+
72+
privatevoidpreNewActivity() {
73+
longstartTime =System.currentTimeMillis();
74+
MainActivitymainActivity =newMainActivity();
75+
Log.d(TAG,"preNewActivity 耗时: " + (System.currentTimeMillis() -startTime));
6876
}
6977

7078

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,33 @@
1-
<?xml version="1.0" encoding="utf-8"?>
2-
<androidx.constraintlayout.widget.ConstraintLayoutxmlns:android="http://schemas.android.com/apk/res/android"
1+
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
32
xmlns:app="http://schemas.android.com/apk/res-auto"
43
xmlns:tools="http://schemas.android.com/tools"
4+
android:id="@+id/constraintLayout"
55
android:layout_width="match_parent"
6-
android:layout_height="match_parent"
7-
tools:context=".MainActivity">
6+
android:layout_height="match_parent">
7+
8+
<ImageView
9+
android:id="@+id/iv"
10+
android:layout_width="40dp"
11+
android:layout_height="40dp"
12+
android:contentDescription="@string/todo"
13+
android:scaleType="centerCrop"
14+
android:src="@drawable/ic_launcher_background"/>
15+
16+
<TextView
17+
android:id="@+id/xml"
18+
style="@style/btn"
19+
android:text="@string/left"/>
820

921
<TextView
10-
android:layout_width="wrap_content"
11-
android:layout_height="wrap_content"
12-
android:text="主页"
13-
app:layout_constraintBottom_toBottomOf="parent"
14-
app:layout_constraintLeft_toLeftOf="parent"
15-
app:layout_constraintRight_toRightOf="parent"
16-
app:layout_constraintTop_toTopOf="parent" />
22+
android:id="@+id/x2c"
23+
style="@style/btn"
24+
android:background="@color/colorPrimary"
25+
android:text="@string/right" />
26+
27+
28+
<ViewStub
29+
android:id="@+id/stub"
30+
android:layout_width="match_parent"
31+
android:layout_height="wrap_content"/>
1732

18-
</androidx.constraintlayout.widget.ConstraintLayout>
33+
</LinearLayout>

‎app/src/main/res/values/strings.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
11
<resources>
22
<stringname="app_name">LaunchTest</string>
3+
4+
5+
<stringname="title">我是标题</string>
6+
<stringname="right">RIGHT</string>
7+
<stringname="left">LEFT</string>
8+
<stringname="todo">TODO</string>
9+
310
</resources>

‎app/src/main/res/values/styles.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,14 @@
1616
<itemname="android:windowBackground">@mipmap/logo</item><!-- 默认背景-->
1717
</style>
1818

19+
20+
<stylename="btn">
21+
<itemname="android:layout_height">50dp</item>
22+
<itemname="android:layout_width">150dp</item>
23+
<itemname="android:textColor">#ffffff</item>
24+
<itemname="android:background">@color/colorAccent</item>
25+
<itemname="android:gravity">center</item>
26+
<itemname="android:textSize">20dp</item>
27+
</style>
28+
1929
</resources>

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp