xposed精简布局xml篇
这个挺难的,小白可以跳过,学习下一篇
这里要用到的xposed接口和修改dex的不一样
接口:IXposedHookInitPackageResources
修改方法也注释在代码里,这里就不详细说了
由于id定位的方法不知道丢哪里去了,这里只能用id名称来定位
声明:这次教程和下一次的有一个缺点就是修改xml前提是在resources.arsc里有该文件信息
首先找到目标xml然后去resources.arsc里搜索文件名获取name的值
name值是main,这里用一点要注意在代码的26行
下面的都在代码的注释里
- package com.mycompany.application2;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.LinearLayout;
- import android.widget.RelativeLayout;
- import android.widget.TextView;
- import de.robv.android.xposed.IXposedHookInitPackageResources;
- import de.robv.android.xposed.IXposedHookLoadPackage;
- import de.robv.android.xposed.XC_MethodHook;
- import de.robv.android.xposed.XposedHelpers;
- import de.robv.android.xposed.callbacks.XC_InitPackageResources;
- import de.robv.android.xposed.callbacks.XC_LayoutInflated;
- import de.robv.android.xposed.callbacks.XC_LoadPackage;
- public class MainHook implements IXposedHookLoadPackage,IXposedHookInitPackageResources {
- //使用IXposedHookInitPackageResources接口
- @Override
- //xml篇
- public void handleInitPackageResources(XC_InitPackageResources.InitPackageResourcesParam loadPackageParam) throws Throwable
- {
- //目标文件main.xml,去resources.arsc里搜索文件,有会员的用Arsc编辑器++搜索取对应文件路径的name值,<path name=”main”>res/layout/main.xml</path>
- //没有会员,的用Arsc编辑器,点搜索资源,输入文件名,找到对应路径的文件,最后一个/的值,com.android.module/layout/main
- //main是在resources.arsc里定的名称,xml前缀名称不一定和resources.arsc里一样,以resources.arsc为准
- //下面第一个参数是目标app包名,第二个是修改布局类型layout,第三个是上面找到的文件名
- loadPackageParam.res.hookLayout(“com.android.module”, “layout”, “main”, new XC_LayoutInflated() {
- @Override
- public void handleLayoutInflated(final LayoutInflatedParam param) throws Throwable {
- //定位xml,第一个参数是id值,第二个参数是搜索类型为id,第三个参数是包名
- LinearLayout a = param.view.findViewById(param.res.getIdentifier(“v”, “id”,”com.android.module”));
- /*下面是a的子级*/
- //a.setVisibility(View.GONE);//隐藏a
- //a.removeViewAt(0);//隐藏a里面的第一个子控件
- //LinearLayout b=(LinearLayout) a.getChildAt(0);//定位a的第一个布局(也可以是控件)
- //b.removeViewAt(0);//隐藏b里面的第一个控件
- /*下面是a的父级*/
- //单纯定位或者用a.setVisibility(View.GONE);隐藏,view就是通用
- //View c= a.getParent();//定位a的上一层
- //View c= a.getParent().getParent();//定位a的上两层,以此类推
- /*下面是a的根布局*/
- //View d= a.getRootView();//根布局是最顶层的布局,没有父布局
- //然后就可以用第一种定位子级的方法修改想改的控件
- }
- });
- }
- /*
- a的根布局getRootView()
- <LinearLayout
- android:orientation=”vertical”
- android:layout_width=”match_parent”
- android:layout_height=”match_parent”>
- //a.getParent()这是a的第二层布局
- <LinearLayout
- android:scrollbars=”none”
- android:layout_width=”match_parent”
- android:layout_height=”match_parent”>
- //a.getParent()这是a的第一层父布局
- <LinearLayout
- android:layout_width=”match_parent”
- android:layout_height=”match_parent”>
- //这是a
- <LinearLayout
- android:id=”@+id/v”
- android:orientation=”vertical”
- android:layout_width=”match_parent”
- android:layout_height=”match_parent”>
- //LinearLayout b=(LinearLayout) a.getChildAt(0);//定位a的第一个子布局
- //a.removeViewAt(0),删除a的第一个子布局
- <LinearLayout
- android:layout_width=”match_parent”
- android:layout_height=”80dp”
- android:background=”@drawable/ListControlBackground”
- android:gravity=”center”
- android:layout_margin=”5dp”>
- //ImageView c=(ImageView) b.getChildAt(0);//定位b的第一个子控件
- //b.removeViewAt(0),删除a的第一个子布局
- <ImageView
- android:layout_width=”70dp”
- android:layout_height=”70dp”
- android:background=”@drawable/AdvertisingIcon”
- android:layout_margin=”10dp” />
- <LinearLayout
- android:layout_width=”wrap_content”
- android:layout_height=”wrap_content”
- android:layout_weight=”1″
- android:layout_marginLeft=”10dp”
- android:orientation=”vertical”>
- <TextView
- android:maxLines=”1″
- android:text=”去广告”
- android:textColor=”#FF0000FF”
- android:layout_width=”wrap_content”
- android:layout_height=”wrap_content”
- android:textSize=”22sp” />
- <TextView
- android:maxLines=”1″
- android:layout_width=”wrap_content”
- android:textColor=”#FF0000FF”
- android:layout_height=”wrap_content”
- android:text=”穿山甲广告,腾讯广告,快手广告,米盟广告” />
- <TextView
- android:maxLines=”1″
- android:layout_width=”wrap_content”
- android:textColor=”#FF0000FF”
- android:layout_height=”wrap_content”
- android:text=”去除部分广告,非通杀” />
- </LinearLayout>
- </LinearLayout>
- </LinearLayout>
- </LinearLayout>
- </LinearLayout>
- </LinearLayout>
- */
- //使用IXposedHookLoadPackage接口
- public void handleLoadPackage(XC_LoadPackage.LoadPackageParam loadPackageParam) throws Throwable {
- //dex篇方法1
- XposedHelpers.findAndHookMethod(“com.android.module.MainActivity”, loadPackageParam.classLoader, “onCreate”,Bundle.class, new XC_MethodHook() {
- @Override
- protected void afterHookedMethod(MethodHookParam param) throws Throwable {
- super.afterHookedMethod(param);
- //变量 a 是自己随便取,后面的就是写找到的字段
- //只有不定位子控件和修改控件,单纯精简,前面View是通用的
- View a = (View) XposedHelpers.getObjectField(param.thisObject,”模块激活”);
- //a.setVisibility(View.GONE);
- }
- }
- );
- //dex篇方法2
- XposedHelpers.findAndHookMethod(“com.android.module.MainActivity”, loadPackageParam.classLoader, “InitializeLayout”, new XC_MethodHook() {
- @Override
- protected void afterHookedMethod(MethodHookParam param) throws Throwable {
- super.afterHookedMethod(param);
- View a = (View) XposedHelpers.callMethod(param.thisObject,”findViewById”,0x7f080006);
- //a.setVisibility(View.GONE);
- }
- }
- );
- //dex篇方法3
- XposedHelpers.findAndHookMethod(“com.android.module.MainActivity”, loadPackageParam.classLoader, “InitializeLayout”, new XC_MethodHook() {
- @Override
- protected void afterHookedMethod(MethodHookParam param) throws Throwable {
- super.afterHookedMethod(param);
- //这里定位子控件不能使用View,使用Java代码里对应的布局LinearLayout
- LinearLayout a = (LinearLayout) XposedHelpers.callMethod(param.thisObject,”findViewById”,0x7f080006);
- //我的听书是第一个控件就填0
- //a.removeViewAt(0);
- }
- }
- );
- }
- }
复制代码
发表评论