SoFunction
Updated on 2025-04-11

Android custom component implementation method

This article describes the implementation method of Android custom component. Share it for your reference. The details are as follows:

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <declare-styleable name="TopBar">
    <attr name="titleText" format="string"/>
    <attr name="titleTextSize" format="dimension"/>
    <attr name="titleTextColor" format="color"/>
    <attr name="leftText" format="string"/>
    <attr name="leftBackground" format="reference|color"/>
    <attr name="leftTextColor" format="color"/>
    <attr name="rightText" format="string"/>
    <attr name="rightBackground" format="reference|color"/>
    <attr name="rightTextColor" format="color"/>
  </declare-styleable>
</resources>

package ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
/**
 * Created by Administrator on 2015/1/8.
 */
public class TopBar extends RelativeLayout{
  private Button leftButton,rightButton;
  private TextView tvTitle;
  private int leftTextColor;
  private Drawable leftBackground;
  private String leftText;
  private int rightTextColor;
  private Drawable rightBackground;
  private String rightText;
  private int titleTextColor;
  private String titleText;
  private float titleTextSize;
  private LayoutParams leftParams,rightParams,titleParams;
  private topBarClickListener listener;
  public interface topBarClickListener{
    public void leftClick();
    public void rightClick();
  }
  public void setOnTopBarClickListener(topBarClickListener listener){
     = listener;
  }
  @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
  public TopBar(final Context context, AttributeSet attrs) {
    super(context, attrs);
    TypedArray ta = (attrs,);
    leftTextColor = (.TopBar_leftTextColor,0);
    leftBackground = (.TopBar_leftBackground);
    leftText = (.TopBar_leftText);
    rightTextColor = (.TopBar_rightTextColor,0);
    rightBackground = (.TopBar_rightBackground);
    rightText = (.TopBar_rightText);
    titleTextColor = (.TopBar_titleTextColor,0);
    titleTextSize = (.TopBar_titleTextSize,0);
    titleText = (.TopBar_titleText);
    ();
    leftButton = new Button(context);
    rightButton = new Button(context);
    tvTitle = new TextView(context);
    (leftTextColor);
    (leftBackground);
    (leftText);
    (rightTextColor);
    (rightBackground);
    (rightText);
    (titleTextColor);
    (titleTextSize);
    (titleText);
    ();
    setBackgroundColor(0xf59563);
    leftParams = new LayoutParams(.WRAP_CONTENT, .WRAP_CONTENT);
    (RelativeLayout.ALIGN_PARENT_LEFT,TRUE);
    addView(leftButton,leftParams);
    rightParams = new LayoutParams(.WRAP_CONTENT, .WRAP_CONTENT);
    (RelativeLayout.ALIGN_PARENT_RIGHT,TRUE);
    addView(rightButton,rightParams);
    titleParams = new LayoutParams(.WRAP_CONTENT, .MATCH_PARENT);
    (RelativeLayout.CENTER_IN_PARENT,TRUE);
    addView(tvTitle,titleParams);
    (new OnClickListener() {
      @Override
      public void onClick(View v) {
        ();
      }
    });
    (new OnClickListener() {
      @Override
      public void onClick(View v) {
        ();
      }
    });
  }
}

activity_main.xml:

&lt;RelativeLayout xmlns:andro
  xmlns:custom="/apk/res-auto"
  xmlns:tools="/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:paddingLeft="@dimen/activity_horizontal_margin"
  android:paddingRight="@dimen/activity_horizontal_margin"
  android:paddingTop="@dimen/activity_vertical_margin"
  android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"&gt;
  &lt;
    android:
    android:layout_width="match_parent"
    android:layout_height="40dp"
    custom:leftBackground="@drawable/blue"
    custom:leftText="Back"
    custom:leftTextColor="#ffffff"
    custom:rightBackground="@drawable/blue"
    custom:rightText="More"
    custom:rightTextColor="#ffffff"
    custom:titleTextColor="#121212"
    custom:titleTextSize="15sp"
    custom:titleText="Custom Title"&gt;
  &lt;/&gt;
&lt;/RelativeLayout&gt;

package ;
import .;
import ;
import ;
import ;
import ;
public class MainActivity extends ActionBarActivity {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    (savedInstanceState);
    setContentView(.activity_main);
    TopBar topBar = (TopBar) findViewById();
    (new () {
      @Override
      public void leftClick() {
        (, "cd--left", Toast.LENGTH_SHORT).show();
      }
      @Override
      public void rightClick() {
        (,"cd--right",Toast.LENGTH_SHORT).show();
      }
    });
  }
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(.menu_main, menu);
    return true;
  }
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in .
    int id = ();
    //noinspection SimplifiableIfStatement
    if (id == .action_settings) {
      return true;
    }
    return (item);
  }
}

I hope this article will be helpful to everyone's Android programming design.