Android Studio布局控件运行时不显示问题求助
Hey there, let's tackle your two Android UI issues step by step—they're actually related, so fixing one will help resolve the other!
The root cause here is almost certainly how you're setting your activity's content view. When you call setContentView(new MyDraw(this)) in MainActivity, you're telling Android to load only your custom MyDraw view as the entire UI for the activity. That means the activity_main.xml layout you designed (with SeekBars and TextViews) isn't being loaded at all—hence why those controls show up in the design preview (which looks at activity_main.xml) but disappear when the app runs.
Here's how to fix it:
- Add your custom view to
activity_main.xmlinstead of replacing the whole layout. Open your XML file and insert theMyDrawview alongside your existing controls. For example, if you're using a verticalLinearLayoutas the root:
Make sure to replace<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <!-- Your existing SeekBars & TextViews --> <SeekBar android:id="@+id/seekBar1" android:layout_width="match_parent" android:layout_height="wrap_content"/> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="SeekBar 1 Value"/> <!-- Insert your custom MyDraw view here --> <com.yourpackage.MyDraw android:id="@+id/my_custom_draw_view" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"/> </LinearLayout>com.yourpackagewith the actual package where yourMyDrawclass is located. - Load the XML layout in
MainActivityinstead of the direct custom view:@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // This loads your full XML layout // If you need to interact with your custom view, grab it like this: MyDraw myDrawView = findViewById(R.id.my_custom_draw_view); } - Quick checks to rule out other issues:
- Ensure none of your SeekBars/TextViews have
android:visibility="gone"orandroid:visibility="invisible"set (this would hide them even if the layout loads). - If using
ConstraintLayout, confirm your controls are properly constrained to the parent or other views—unconstrained views can sometimes get pushed off-screen.
- Ensure none of your SeekBars/TextViews have
This is a direct side effect of the first problem. When you set MyDraw as the content view, that's the only view in your activity's UI hierarchy. The Button you defined in activity_main.xml doesn't exist in that hierarchy, so findViewById can't locate it.
The fix aligns with Issue 1:
- Load
activity_main.xmlas your content view (as shown above). This includes both yourMyDrawview and the Button in the UI hierarchy. - Grab the Button reference normally:
Button myButton = findViewById(R.id.button); myButton.setOnClickListener(v -> { // Add your click logic here });
If you really need to keep MyDraw as the root view (not recommended for most cases, since XML layouts are easier to design/maintain), you'd have to add the Button programmatically to the MyDraw view:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); MyDraw myDraw = new MyDraw(this); setContentView(myDraw); // Create Button programmatically Button button = new Button(this); button.setId(R.id.my_programmatic_button); // Define this id in res/values/ids.xml first button.setText("Tap Me"); // Set layout params (adjust based on MyDraw's parent layout type) LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT ); myDraw.addView(button, params); // Now you can find it Button foundButton = findViewById(R.id.my_programmatic_button); }
But again, sticking with the XML layout approach is better for readability and maintainability.
内容的提问来源于stack exchange,提问作者user4432581

