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

Android新手求助:XML中71个按钮仅显示31号及以后,需从1号起

Troubleshooting Your Android Button Display & Click Issues

Hey there! Let's work through this problem step by step—manually coding 71 buttons is no small feat, especially as a new Android dev, so kudos for putting in that work! 😊

First, Let's Figure Out Why Buttons Start at 31 Instead of 1

Here are the most likely culprits to check:

  • Check your parent layout properties

    • If you're using a LinearLayout to hold the buttons, double-check its orientation is set to vertical (you want top-to-bottom order). Also make sure you haven't set gravity="bottom" or layout_gravity="bottom" on either the LinearLayout or the buttons themselves—this would push all buttons to the bottom, making the first ones hidden if the screen isn't tall enough.
    • If you're wrapping the layout in a ScrollView, add android:fillViewport="true" to the ScrollView tag. Without this, if the total content height is shorter than the screen, the layout might not align to the top, leading to unexpected starting positions.
  • Verify the first 30 buttons aren't hidden

    • Look through the XML for your first 30 buttons—did you accidentally set android:visibility="gone" or android:visibility="invisible" on any of them? A gone view takes up no space, so the layout would skip right to the first visible button (31 in your case).
    • Check the layout_height and text of the first button ("Dhaka Division"). If layout_height is set to 0dp without a weight, or if the button's text is empty (maybe a typo in the string resource), the button would be invisible or have zero height, making it look like the list starts at 31.
  • Check for unintended scroll behavior

    • Open your MainActivity.java and look for any code that calls scrollTo(), smoothScrollTo(), or similar methods on your ScrollView/parent layout. If you accidentally added code that scrolls the view to a specific position, that could be jumping you to button 31.

Fixing the 1st Button Click Issue

For the click problem, these are the common fixes:

  • Double-check button IDs and click bindings

    • Make sure the android:id for your "Dhaka Division" button is correctly defined in XML (e.g., @+id/btn_dhaka_division), and that you're using the exact same ID in findViewById() in your MainActivity. A typo here would mean the click listener never gets attached, leading to no response when you tap it.
    • If you're using android:onClick="methodName" in the XML, confirm that your MainActivity has a method with that exact name, and it accepts a View parameter (like public void onDhakaButtonClick(View v) {}).
  • Check for null pointers

    • Add a quick log statement or debug check after findViewById for the first button to make sure it's not returning null. If it is, that means the ID doesn't match, and the click listener isn't being set at all.

Pro Tip for Future Projects

Manually writing 71 buttons is tedious and error-prone! For large lists like this, use a RecyclerView instead. You can create an array of all your button texts, then use an Adapter to dynamically generate each button row. This will make your code cleaner, easier to maintain, and less likely to have layout issues.

内容的提问来源于stack exchange,提问作者Jahidul Islam

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 07:22:17