《Android第一行代码》first reading 五

我们所用的所有控件都是直接或间接继承自View的,所用的所有布局都是直接或间接继承自ViewGroup的。

常用控件和布局的继承关系.png

View是Android中最基本的一种UI组件,它可以在屏幕上绘制一块矩形区域,并能响应这块区域的各种事件,因此,我们使用的各种控件其实就是在View的基础上又添加了各自特有的功能。而ViewGroup则是一种特殊的View,它可以包含很多子View和子ViewGroup,是一个用于放置控件和布局的容器。

引入布局

  1. title.xml
    新建一个title布局文件,准备将布局文件引入main.xml中
<LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:background="@color/colorTitle">
    <Button
        android:id="@+id/title_back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_margin="5dp"
        android:background="@color/colorBack"
        android:text="Back"
        android:textColor="#fff"/>
    <TextView
        android:id="@+id/title_text"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:layout_weight="1"
        android:text="Title Text"
        android:textColor="#fff"
        android:textSize="24sp"/>
    <Button
        android:id="@+id/title_edit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_margin="5dp"
        android:background="@color/colorEdit"
        android:text="Edit"
        android:textColor="#fff"/>
</LinearLayout>
  1. main.xml
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="soft.wc.com.uicustomviews.MainActivity">
    <include layout="@layout/title" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:text="Hello World!" />
</LinearLayout>

<include layout="@layout/title" />我们只用include语句就可将title布局引入进来。

  1. BaseActivity
public abstract class BaseActivity extends AppCompatActivity{
    protected Context mContext;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.mContext = this;
        int layoutId = getLayoutId();
        View contentView = LayoutInflater.from(mContext).inflate(layoutId,null,false);
        setContentView(contentView);
        hideActionBar();
        initView();
    }
    /**
     *初始化界面
     */
    public abstract void initView();
    /**
     *隐藏标题栏
     */
    public void hideActionBar(){
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null){
            actionBar.hide();
        }
    }
    /**
     * 获取布局文件的id
     *
     * @return
     */
    public abstract int getLayoutId();
}

getSupportActionBar()方法来获得ActionBar实例,然后再调用ActionBar的hide()方法将标题栏隐藏起来。

创建自定义控件

  1. 新建一个TitleLayout类继承LinearLayout:
public class TitleLayout extends LinearLayout{
    public TitleLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.title,this);
    }
}

通过LayoutInflateer的from()方法可以构建出一个LayoutInflater对像,然后调用inflate()方法就可以动态的加载一个布局文件,inflate()方法接受两个参数,一个是要加载的布局的id,一个是布局放置的父文件。

  1. 在main.xml中引入自定义控件:
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="soft.wc.com.uicustomviews.MainActivity">
    <soft.wc.com.uicustomviews.TitleLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 11,872评论 0 17
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 176,231评论 25 709
  • Day1: 在代码中通过R.string.hello_world可以获得该字符串的引用; 在XML中通过@stri...
    冰凝雪国阅读 5,360评论 0 5
  • java基础题 要是java笔试遇到这个题目,你知道答案吗? 这道题目不难,java基础扎实的,一看便知答案!BU...
    檀木丁阅读 3,788评论 3 3
  • 很多人只擅长和自己相处,责怪自己,也怜悯自己。 再见到cookie的时候,黑色及腰的长发只留在回忆里,12月的冷风...
    云打伞阅读 3,865评论 0 5

友情链接更多精彩内容