Android:2分钟创建一个好看的关于页面

写在前面

通常关于页面是我们开发的时候最容易忽视的页面,一般我都是留在最后才去设计关于页面,但是虽然关于页面并不起眼但是也不能忽视它的作用,所以我就在想能不能在很短时间内创建一个蛮好看的关于页面呢,我在Github找到了这个开源库,介绍里号称可以在两分钟内创建一个好看的关于页面。

这里写图片描述

用法

这个开源库的用法也很简单

首先在app/build.gradle文件中声明这个库的依赖:

dependencies {
    compile 'com.github.medyo:android-about-page:1.2'
}

然后再修改关于(例如是MainActivity)页面的代码:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        View aboutPage = new AboutPage(this)
                .isRTL(false)
                .setImage(R.drawable.girl)//图片
                .setDescription("道理我都懂,可我就是不听啊")//介绍
                .addItem(new Element().setTitle("Version 1.0"))
                .addGroup("与我联系")
                .addEmail("zhaoweihaochn@foxmail.com")//邮箱
                .addWebsite("http://zhaoweihao.me")//网站
                .addPlayStore("com.example.abouttest")//应用商店
                .addGitHub("zhaoweihaoChina")//github
                .create();

        setContentView(aboutPage);
    }
}

运行到模拟器
这里写图片描述

这样一个比较好看的关于页面就创建完毕了

这个开源库让我们建立关于页面的速度快了不是一点

更多

我们一般不会这样添加一个关于页面,因为现在要用到Toolbar而不是默认的Actionbar

先修改values/style 换成NoActionBar主题

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
       ......
    </style>

</resources>

所以我们先在app/res/layout里面新建一个about_layout.xml


<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        ></android.support.v7.widget.Toolbar>

    </android.support.design.widget.AppBarLayout>

    <RelativeLayout
        android:id="@+id/relativeLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        >


    </RelativeLayout>

</android.support.design.widget.CoordinatorLayout>

然后在包下创建AboutActivity


package com.example.abouttest;

import android.os.Bundle;
......

/**
 * Created by Administrator on 2017/5/2.
 */

public class AboutActivity extends AppCompatActivity{

    private RelativeLayout relativeLayout;

    private Toolbar toolbar;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.about_layout);

        initViews();

        View aboutPage = new AboutPage(this)
                .isRTL(false)
                .setImage(R.drawable.girl)//图片
                .setDescription("道理我都懂,可我就是不听啊")//介绍
                .addItem(new Element().setTitle("Version 1.0"))
                .addGroup("与我联系")
                .addEmail("zhaoweihaochn@foxmail.com")//邮箱
                .addWebsite("http://zhaoweihao.me")//网站
                .addPlayStore("com.example.abouttest")//应用商店
                .addGitHub("zhaoweihaoChina")//github
                .create();

        relativeLayout.addView(aboutPage);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case android.R.id.home:
                finish();
                break;
            default:

        }
        return true;
    }

    private void initViews(){
        relativeLayout= (RelativeLayout) findViewById(R.id.relativeLayout);
        toolbar= (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        ActionBar actionBar=getSupportActionBar();
        if(actionBar!=null){
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
    }


}


最后别忘了在AndroidManifest.xml中声明AboutActivity

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.abouttest">

    <application
       ......
        <activity android:name=".AboutActivity"
        android:label="关于"
        >
        </activity>
    </application>

</manifest>

运行到手机,查看

这里写图片描述

虽然效果看上去和上面没有区别,但是我们的aboutpage是在relativelayout里面的,ActionBar也改为Toolbar

添加自定义Element

以APP版本为例子 :

Element versionElement = new Element();
versionElement.setTitle("Version 6.2");
addItem(versionElement)

当前可用的Element类型

setTitle(String)    //设置element的标题
setIconTint(Int)    //设置element的颜色
setIconDrawable(Int)    //设置element的图标
setValue(String)    //Set Element value like Facebook ID
setIntent(Intent)   //Set intent to be called on onClickListener
setGravity(Gravity) //Set a Gravity for the element
setOnClickListener(View.OnClickListener)    //如果Intent不是你想要的,可以在这里重写

写在最后

如果你阅读本文后受益匪浅,可以不妨请我喝杯咖啡[微信扫描下发二维码向我转账]

这里写图片描述

联系方式

  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,你需要在布局文件中添加一个TextView来显示日期、温度等信息。然后,你可以使用一些现成的库来获取天气数据,例如OpenWeatherMap API。 以下是一个简单的示例布局文件: ``` <?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="match_parent"> <TextView android:id="@+id/dateTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="24sp" android:textColor="@color/black" android:text="Today's Date" android:layout_marginTop="16dp" android:layout_centerHorizontal="true"/> <TextView android:id="@+id/temperatureTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="48sp" android:textColor="@color/red" android:text="Temperature" android:layout_below="@id/dateTextView" android:layout_centerHorizontal="true" android:layout_marginTop="32dp"/> </RelativeLayout> ``` 在这个布局文件中,我们添加了两个TextView。第一个TextView用于显示日期,第二个TextView用于显示温度。我们还使用了RelativeLayout来控制它们的位置。 接下来,你需要在你的Activity中使用这个布局文件,并设置日期和温度值。你可以使用Java中的SimpleDateFormat来格式化日期,然后使用OpenWeatherMap API来获取天气数据。以下是一个简单的示例代码: ``` public class WeatherActivity extends AppCompatActivity { private TextView dateTextView, temperatureTextView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_weather); dateTextView = findViewById(R.id.dateTextView); temperatureTextView = findViewById(R.id.temperatureTextView); // 获取当前日期并格式化 SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, MMM d"); String currentDate = dateFormat.format(new Date()); // 获取天气数据并设置温度值 WeatherApiClient weatherApiClient = new WeatherApiClient(); weatherApiClient.getTemperature(new WeatherApiClient.Callback() { @Override public void onSuccess(double temperature) { temperatureTextView.setText(String.format("%.1f℃", temperature)); } @Override public void onError(String message) { temperatureTextView.setText("N/A"); } }); // 设置日期值 dateTextView.setText(currentDate); } } ``` 在这个示例代码中,我们首先获取当前日期并使用SimpleDateFormat格式化。然后,我们使用WeatherApiClient类来获取天气数据,并在回调中设置温度值。最后,我们设置日期值。请注意,这里使用了一个回调来处理异步获取天气数据的过程。 这只是一个简单的示例,你需要根据你的具体需求来修改和完善它。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值