You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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!

Issue 1: SeekBar & TextView visible in design panel but not at runtime

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:

  1. Add your custom view to activity_main.xml instead of replacing the whole layout. Open your XML file and insert the MyDraw view alongside your existing controls. For example, if you're using a vertical LinearLayout as the root:
    <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>
    
    Make sure to replace com.yourpackage with the actual package where your MyDraw class is located.
  2. Load the XML layout in MainActivity instead 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);
    }
    
  3. Quick checks to rule out other issues:
    • Ensure none of your SeekBars/TextViews have android:visibility="gone" or android: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.
Issue 2: Can't find Button via findViewById after using MyDraw as ContentView

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:

  1. Load activity_main.xml as your content view (as shown above). This includes both your MyDraw view and the Button in the UI hierarchy.
  2. 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 03:31:19