Android新手求助:XML中71个按钮仅显示31号及以后,需从1号起
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
LinearLayoutto hold the buttons, double-check itsorientationis set tovertical(you want top-to-bottom order). Also make sure you haven't setgravity="bottom"orlayout_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, addandroid: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.
- If you're using a
Verify the first 30 buttons aren't hidden
- Look through the XML for your first 30 buttons—did you accidentally set
android:visibility="gone"orandroid:visibility="invisible"on any of them? Agoneview takes up no space, so the layout would skip right to the first visible button (31 in your case). - Check the
layout_heightand text of the first button ("Dhaka Division"). Iflayout_heightis set to0dpwithout 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.
- Look through the XML for your first 30 buttons—did you accidentally set
Check for unintended scroll behavior
- Open your
MainActivity.javaand look for any code that callsscrollTo(),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.
- Open your
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:idfor your "Dhaka Division" button is correctly defined in XML (e.g.,@+id/btn_dhaka_division), and that you're using the exact same ID infindViewById()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 aViewparameter (likepublic void onDhakaButtonClick(View v) {}).
- Make sure the
Check for null pointers
- Add a quick log statement or debug check after
findViewByIdfor the first button to make sure it's not returningnull. If it is, that means the ID doesn't match, and the click listener isn't being set at all.
- Add a quick log statement or debug check after
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

