Android Studio中如何同时显示自定义MyView与activity_customer布局?
Hey there! I get what you're dealing with—calling setContentView() twice just replaces the entire screen with whatever you set last, right? That's why only one of your views shows up. Let's fix this so you can have both your activity_customer layout and custom MyView on the same screen.
Why Your Current Approach Isn't Working
Every time you call setContentView(), Android replaces the entire root view of your activity with the new one you pass in. So whichever call comes second overwrites the first completely. We need to combine them into a single view hierarchy instead.
Solution 1: Add MyView Directly to Your XML Layout
This is the simplest approach if you want a fixed layout. Just modify your activity_customer.xml to include your custom MyView as a child of the root RelativeLayout. Make sure to use the full class name of your MyView (including the package):
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.asdf.catchtheball.main"> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:text="Button" /> <!-- Add your custom MyView here (replace with your full class path) --> <com.example.asdf.catchtheball.MyView android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@+id/button" /> <!-- Adjust position as needed --> </RelativeLayout>
Then in your Customer.java onCreate() method, you only need one line:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_customer); // This loads both the Button and MyView }
Solution 2: Dynamically Add MyView to the Layout in Code
If you prefer to keep your XML layout as-is and add MyView programmatically, you can do this by getting the root layout of your activity and adding MyView to it:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Load your XML layout first setContentView(R.layout.activity_customer); // Get the root layout container (our RelativeLayout from XML) RelativeLayout rootLayout = findViewById(android.R.id.content); // Create an instance of your MyView MyView myView = new MyView(this); // Set layout parameters for MyView (adjust size/position here) RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT ); // Example: Place MyView below the Button layoutParams.addRule(RelativeLayout.BELOW, R.id.button); myView.setLayoutParams(layoutParams); // Add MyView to the root layout rootLayout.addView(myView); }
Important Note: Make Sure MyView Has the Right Constructors
For both solutions to work, your MyView class needs the correct constructors. At minimum, include these:
public class MyView extends View { // For programmatic creation public MyView(Context context) { super(context); init(); // Call your initialization code here } // For XML inflation (required if you use Solution 1) public MyView(Context context, AttributeSet attrs) { super(context, attrs); init(); // Call your initialization code here } private void init() { // Your custom view setup goes here (e.g., paint, listeners) } }
Either solution will let you see both your button from the XML layout and your custom MyView on the same screen. Pick the one that fits your workflow best!
内容的提问来源于stack exchange,提问作者Dijital Dünya

