可重复使用的布局安卓类布局、重复使用、安卓类

2023-09-07 09:17:05 作者:逗癌晚期

我有一个可重复使用的布局被称为标题

 < XML版本=1.0编码=UTF-8&GT?;
 

 <的TextView
    机器人:ID =@ + ID / title_text
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_alignParentLeft =真
    机器人:layout_alignParentRight =真
    机器人:layout_centerVertical =真
    机器人:重力=中心
    机器人:文本=@字符串/ lesson_title
    机器人:TEXTSIZE =@扪/标题/>

<按钮
    机器人:ID =@ + ID / title_book
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_alignParentBottom =真
    机器人:layout_alignParentRight =真
    机器人:layout_alignParentTop =真
    机器人:的onClick =的onClick
    机器人:文本=@字符串/书/>

<按钮
    机器人:ID =@ + ID / title_buy
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_alignParentBottom =真
    机器人:layout_alignParentRight =真
    机器人:layout_alignParentTop =真
    机器人:的onClick =的onClick
    机器人:文本=@字符串/买入
    机器人:能见度=水涨船高/>
 

我有这个在我的大多数活动和工作正常。

安卓学习笔记10 常用布局 相对布局

要处理的点击和标题文字我不得不这样做的每一项活动

  TextView的标题=(TextView中)findViewById(R.id.title_text);
title.setText(R.string.some_title);
巴顿书=(按钮)findViewById(R.id.title_book);
按钮团购=(按钮)findViewById(R.id.title_buy);
 

和点击次数

 公共无效的onClick(视图v){

    开关(v.getId()){

    案例R.id.title_book:
            Toast.makeText(getBaseContext(),书,Toast.LENGTH_SHORT).show();
        打破;

    案例R.id.title_buy:
            Toast.makeText(getBaseContext(),买入,Toast.LENGTH_SHORT).show();
        打破;
    }
}
 

有没有一种方法,我可以创建一个类来处理呢?它可以有,我可以从活动呼吁建立对标题的文字,只是因为按钮会做同样的功能,每次一个init方法。

这是我所试图做基于乌迪答案

 公共类CustomTitle扩展RelativeLayout的{

私人语境mContext;
私人LayoutInflater layoutInflater;
私人ViewHolder myViewHolder;
多行文字字符串=你好;

公共CustomTitle(上下文的背景下){
    超(上下文);
    this.mContext =背景;
    //this.mText =文本;

    膨胀();
    bindViews();
}

私人无效的inflate(){

    如果(layoutInflater == NULL){
        layoutInflater =(LayoutInflater)mContext
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    layoutInflater.inflate(com.callan.app.R.layout.title,这一点,真正的);
}

私人无效bindViews(){
    //这里绑定所有意见

    如果(myViewHolder == NULL){
        myViewHolder =新ViewHolder();
    }

    myViewHolder.title_text =(TextView中)findViewById(com.callan.app.R.id.title_text);
    myViewHolder.book =(按钮)findViewById(com.callan.app.R.id.title_book);
    myViewHolder.buy =(按钮)findViewById(com.callan.app.R.id.title_buy);

    如果(Callan.getMyCredit()大于0){

        myViewHolder.buy.setVisibility(View.GONE);
        myViewHolder.book.setVisibility(View.VISIBLE);
    }其他{

        myViewHolder.buy.setVisibility(View.VISIBLE);
        myViewHolder.book.setVisibility(View.GONE);
    }

    myViewHolder.title_text.setText(MTEXT);
}

类ViewHolder {

    TextView的title_text;
    巴顿的书;
    按钮购买;
}

公共无效的onClick(视图v){

    开关(v.getId()){

    案例com.callan.app.R.id.title_book:
            Toast.makeText(mContext,书,Toast.LENGTH_SHORT).show();
        打破;

    案例com.callan.app.R.id.title_buy:
            Toast.makeText(mContext,买入,Toast.LENGTH_SHORT).show();
        打破;
    }
}
 

}

和我的XML我有

 < com.callan.custom_views.CustomTitle的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:重力=center_vertical
机器人:方向=垂直>

//我的看法
 

然后在我的活动的onCreate后,我有

  CustomTitle cTitle =新CustomTitle(本);
 

异常

  04-30 14:24:44.440:E / AndroidRuntime(7184):java.lang.RuntimeException的:无法启动的活动ComponentInfo {com.callm.app / com.callan。 app.Lessons}:android.view.InflateException:二进制XML文件中的行#2:错误充气类com.callm.custom_views.CustomTitle
 04-30 14:24:44.440:E / AndroidRuntime(7184):android.view.InflateException:二进制XML文件中的行#2:错误充气类com.callm.custom_views.CustomTitle所致
 

解决方案

您可以创建一个扩展的FrameLayout /或任何你想要的东西的自定义类。 在它的构造函数创建2个方法:膨胀()和bindViews()

 公共类CustomView扩展的FrameLayout {

私人语境mContext;
私人LayoutInflater layoutInflater;
//这里所有的观点

公共CustomView(上下文的背景下){
    超(上下文);
    this.mContext =背景;
    膨胀();
    bindViews();

}

私人无效的inflate(){
    layoutInflater =(LayoutInflater)mContext
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    layoutInflater.inflate(R.layout.your_xml_layout,这一点,真正的);
}

私人无效bindViews(){
    //这里绑定所有意见
}
}
 

添加你的方法,你就可以你需要的地方使用这个类。

I have a reusable layout called title

<?xml version="1.0" encoding="utf-8"?>

<TextView
    android:id="@+id/title_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
    android:gravity="center"
    android:text="@string/lesson_title"
    android:textSize="@dimen/title" />

<Button
    android:id="@+id/title_book"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:onClick="onClick"
    android:text="@string/book" />

<Button
    android:id="@+id/title_buy"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:onClick="onClick"
    android:text="@string/buy"
    android:visibility="gone" />

I include this in most of my activities and that works fine.

To handle the clicks and the title text I have to do this in every activity

TextView title = (TextView) findViewById(R.id.title_text);
title.setText(R.string.some_title);
Button book = (Button) findViewById(R.id.title_book);
Button buy = (Button) findViewById(R.id.title_buy);

and for clicks

public void onClick(View v) {

    switch (v.getId()) {

    case R.id.title_book:
            Toast.makeText(getBaseContext(), "book", Toast.LENGTH_SHORT).show();
        break;

    case R.id.title_buy:
            Toast.makeText(getBaseContext(), "buy", Toast.LENGTH_SHORT).show();
        break;
    }
}

Is there a way I can create a class to handle this? It could have an init method which I could call from the activities to set up the text on the title only because the buttons will do the same function everytime.

This is what I have tried to do based on the answer by Udi

public class CustomTitle extends RelativeLayout{

private Context mContext;
private LayoutInflater layoutInflater;
private ViewHolder myViewHolder;
String mText = "hello";

public CustomTitle(Context context) {
    super(context);
    this.mContext = context;
    //this.mText = text;

    inflate();
    bindViews();
}

private void inflate() {

    if(layoutInflater == null){
        layoutInflater = (LayoutInflater) mContext
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    layoutInflater.inflate(com.callan.app.R.layout.title, this, true);      
}

private void bindViews() {
    // bind all views here

    if(myViewHolder == null){
        myViewHolder = new ViewHolder();
    }

    myViewHolder.title_text = (TextView) findViewById(com.callan.app.R.id.title_text);
    myViewHolder.book = (Button) findViewById(com.callan.app.R.id.title_book);
    myViewHolder.buy = (Button) findViewById(com.callan.app.R.id.title_buy);

    if(Callan.getMyCredit() > 0){

        myViewHolder.buy.setVisibility(View.GONE);
        myViewHolder.book.setVisibility(View.VISIBLE);
    }else{

        myViewHolder.buy.setVisibility(View.VISIBLE);
        myViewHolder.book.setVisibility(View.GONE);
    }

    myViewHolder.title_text.setText(mText);     
}

class ViewHolder{

    TextView title_text;
    Button book;
    Button buy;
}

public void onClick(View v) {

    switch (v.getId()) {

    case com.callan.app.R.id.title_book:
            Toast.makeText(mContext, "book", Toast.LENGTH_SHORT).show();
        break;

    case com.callan.app.R.id.title_buy:
            Toast.makeText(mContext, "buy", Toast.LENGTH_SHORT).show();
        break;
    }
}

}

and in my xml I have

<com.callan.custom_views.CustomTitle xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:orientation="vertical" >

//my views

Then in my activity I have after onCreate

CustomTitle cTitle = new CustomTitle(this);

Exception

04-30 14:24:44.440: E/AndroidRuntime(7184): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.callm.app/com.callan.app.Lessons}:       android.view.InflateException: Binary XML file line #2: Error inflating class        com.callm.custom_views.CustomTitle
 04-30 14:24:44.440: E/AndroidRuntime(7184): Caused by: android.view.InflateException:       Binary XML file line #2: Error inflating class com.callm.custom_views.CustomTitle

解决方案

You can create custom class which extends FrameLayout/or anything else you want. in it's constructor create 2 methods: inflate() and bindViews()

public class CustomView extends FrameLayout {

private Context mContext;
private LayoutInflater layoutInflater;
// All views here

public CustomView (Context context) {
    super(context);
    this.mContext = context;
    inflate();
    bindViews();

}

private void inflate() {
    layoutInflater = (LayoutInflater) mContext
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    layoutInflater.inflate(R.layout.your_xml_layout, this, true);
}

private void bindViews() {
    // bind all views here
}
}

add your methods and you'll be able to use this class wherever you need.