简体   繁体   中英

How can I make my own widget to be used in an XML layout file

I have seen xml files that use custom widgets, for example, the google sign in button...

<com.google.android.gms.common.SignInButton
            android:id="@+id/sign_in_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:foreground="@drawable/log_in" />

I found that if you make a class in your package that extends ViewGroup , you can say for example... <com.example.myApp.MyCustomViews etc etc.

So right now I have this class...

public class ChatUILayout extends ViewGroup {

    public ChatUILayout(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        // TODO Auto-generated method stub

    }

}

I'd like this chat user interface layout to contain an EditText , TextView , and a Button when I refer to it from xml, how do I implement this?

From my understanding you would need to use a layout inflater.

inflate the xml (ChatUILayout) and when you inflate that view, you will use a findViewById(R.id.YourEditText), findViewById(R.id.YourTextView) etc. and then set the necessary values and click handlers.

View view = LayoutInflater.from(getBaseContext()).inflate(R.layout.ChatUILayout),
            null);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM