Android常用技巧汇总(持续更新)

Posted zstar-_

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android常用技巧汇总(持续更新)相关的知识,希望对你有一定的参考价值。

本篇博文中用于汇总android开发中的各种通用技巧,方便使用时复制

启动页

新建Activity,命名为Splash
跳转时间设为2000ms
Splash.java

public class Splash extends AppCompatActivity 
    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);

        new android.os.Handler().postDelayed(new Runnable() 
            @Override
            public void run() 
                Intent mainIntent = new Intent(Splash.this,MainActivity.class);
                Splash.this.startActivity(mainIntent);
                Splash.this.finish();
            
        ,2000);
    


在Manifest中设置启动和主题

        <activity android:name=".Splash"
            android:theme="@style/ThemeSplash">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

在themes.xml中添加主题,设置通知栏颜色和背景图片

    <style name="ThemeSplash" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="colorPrimaryDark">@color/bar</item>
        <item name="android:windowBackground">@drawable/qidong</item>
    </style>

插入图片

设置android:adjustViewBounds="true",这可以让插入图片时上下不存在空白

        <ImageView
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:src="@drawable/logo"
            android:adjustViewBounds="true" />

隐藏标题栏

两种方式,均在onCreate中添加
方式一:

requestWindowFeature(Window.FEATURE_NO_TITLE);  // 隐藏标题栏

方式二:

if (getSupportActionBar() != null)
    getSupportActionBar().hide();

修改Activity切换效果

默认的切换效果是右边划入,左边划出,可用下面的方式来修改切换动画。
这里以透明度和缩放的组合动画为例,新建动画文件goat_in.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <alpha xmlns:android="http://schemas.android.com/apk/res/android"
        android:fromAlpha="0.5"
        android:toAlpha="1.0"
        android:duration="300">
    </alpha>

    <scale xmlns:android="http://schemas.android.com/apk/res/android"
        android:fromXScale="0.4"
        android:toXScale="1"
        android:fromYScale="0.4"
        android:toYScale="1"
        android:pivotX="50%"
        android:pivotY="25%"
        android:duration="300">
    </scale>
</set>

再在切换Activity时添加overridePendingTransition

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) 
        View view = inflater.inflate(R.layout.fragment_1, container, false);
        s1 = view.findViewById(R.id.s1);
        s1.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View view) 
                Intent i = new Intent(getActivity(), Goat_preview.class);
                startActivity(i);
                getActivity().overridePendingTransition(R.anim.goat_in,0);
            
        );
        return view;
    

更多动画效果可参考:https://blog.csdn.net/u010126792/article/details/85766747

形状Shape相关属性

<shape>
     <!-- 实心 -->
     <solid android:color="#ff9d77"/>
     <!-- 渐变 -->
     <gradient
         android:startColor="#ff8c00"
         android:endColor="#FFFFFF"
         android:angle="270" />
     <!-- 描边 -->
     <stroke
         android:width="2dp"
         android:color="#dcdcdc" />
     <!-- 圆角 -->
     <corners
         android:radius="2dp" />
     <!-- 单独设置圆角 -->
<!--     <corners-->
<!--     android:topLeftRadius="10dp"-->
<!--     android:topRightRadius="10dp"/>-->
     <padding
         android:left="10dp"
         android:top="10dp"
         android:right="10dp"
         android:bottom="10dp" />
 </shape>

共享元素动画

单个元素:
在需要共享的元素添加android:transitionName,名称自拟,两个相同元素要对应。
Activity1:

        <ImageView
            android:id="@+id/share"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:src="@drawable/logo"
            android:adjustViewBounds="true"
            android:transitionName="shareElement"/>

Activity2:

        <ImageView
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:src="@drawable/logo"
            android:adjustViewBounds="true"
            android:transitionName="shareElement"/>

Activity1.java

ImageView share = findViewById(R.id.share);


Bundle bundle = ActivityOptionsCompat.makeSceneTransitionAnimation(Splash.this,share,"shareElement").toBundle();
Intent i = new Intent(Splash.this,Welcome.class);
Splash.this.startActivity(i,bundle);

设置状态栏颜色

onCreate中添加:

Window window = this.getWindow();
window.setStatusBarColor(Color.WHITE);

按钮监听

单个按钮监听

Button bk = findViewById(R.id.dz_back);
bk.setOnClickListener(new View.OnClickListener() 
    @Override
    public void onClick(View v) 
        finish();
    
);

多个按钮监听

Button btn_register = findViewById(R.id.btn_rgs);
ImageView iv_back = findViewById(R.id.iv_back);
iv_back.setOnClickListener(this);
btn_register.setOnClickListener(this);

@Override
public void onClick(View v) 
    switch (v.getId()) 
        case R.id.iv_back:
            Intent intent = new Intent(RegisteredActivity.this, Welcome.class);
            startActivity(intent);
            finish();
            break;
        case R.id.btn_rgs:
             .....
            break;
    

页面跳转

Activity->Activity:

ImageView btn1 = findViewById(R.id.btn1);
btn1.setOnClickListener(new View.OnClickListener() 
    @Override
    public void onClick(View v) 
        Intent i = new Intent(MainActivity.this, shouye.class);
        startActivity(i);
    
);

Fragment->Activity:

public class pageFragment1 extends Fragment  
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) 
        View view = inflater.inflate(R.layout.fragment_page_fragment1, container, false);

        TextView jr = view.findViewById(R.id.jrtj);
        jr.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View view) 
                Intent i = new Intent(getActivity(), test.class);
                startActivity(i);
            
        );
        return view;
    

以上是关于Android常用技巧汇总(持续更新)的主要内容,如果未能解决你的问题,请参考以下文章

python常用函数技巧汇总

python常用函数技巧汇总

python使用小技巧汇总(持续更新)

Android 系统中,那些能大幅提高工作效率的 API 汇总(持续更新中...)

LeetCode算法技巧汇总 -- 持续更新,学无止境!

LeetCode算法技巧汇总 -- 持续更新,学无止境!