【问题标题】:Android Buttons Overlap Each Other (Correct Answer Found)Android 按钮相互重叠(找到正确答案)
【发布时间】:2014-07-14 05:56:00
【问题描述】:

我只是有一个关于 android 按钮布局的小问题。我的应用程序中有按钮,我在所有 4 面都设置为 -5dip。我知道我可以弄乱这些数字,但我想做的就是让它们在彼此相邻的地方填满一个没有重叠的地方。我对应用程序构建非常陌生,任何帮助都会很好。

我被引用的 .xml 文件。

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main_menuScrollView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:context="com.example.comprehensivezeldachecklist.MainMenu$PlaceholderFragment" >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
        <!-- Zelda -->
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content" 
            android:layout_marginLeft="0dip"
            android:layout_marginRight="0dip"
            android:layout_marginTop="0dip"
            android:layout_marginBottom="0dip"
            android:text="@string/Zelda" />
        <!-- Zelda II -->
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="0dip"
            android:layout_marginLeft="0dip"
            android:layout_marginRight="0dip"
            android:layout_marginTop="0dip"
            android:text="@string/Zelda2" />
        <!-- A Link to the Past -->
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="0dip"
            android:layout_marginRight="0dip"
            android:layout_marginTop="0dip"
            android:layout_marginBottom="0dip"
            android:text="@string/LinkToThePast" />    
        <!-- Link's Awakening -->
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="0dip"
            android:layout_marginRight="0dip"
            android:layout_marginTop="0dip"
            android:layout_marginBottom="0dip"
            android:text="@string/LinksAwakening" />            
        <!-- Link's Awakening DX -->
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="0dip"
            android:layout_marginRight="0dip"
            android:layout_marginTop="0dip"
            android:layout_marginBottom="0dip"
            android:text="@string/LinksAwakeningDX" />    
        <!-- Ocarina of Time -->
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="0dip"
            android:layout_marginRight="0dip"
            android:layout_marginTop="0dip"
            android:layout_marginBottom="0dip"
            android:text="@string/OcarinaOfTime" 
            android:onClick= "OoTMenu"/>    
        <!-- Majora's Mask -->
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="0dip"
            android:layout_marginRight="0dip"
            android:layout_marginTop="0dip"
            android:layout_marginBottom="0dip"
            android:text="@string/MajorasMask" />    
        <!-- Oracle of Ages -->
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="0dip"
            android:layout_marginRight="0dip"
            android:layout_marginTop="0dip"
            android:layout_marginBottom="0dip"
            android:text="@string/OracleOfAges" />    
        <!-- Oracle of Seasons -->
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="0dip"
            android:layout_marginRight="0dip"
            android:layout_marginTop="0dip"
            android:layout_marginBottom="0dip"
            android:text="@string/OracleOfSeasons" />    
        <!-- Four Swords -->
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="0dip"
            android:layout_marginRight="0dip"
            android:layout_marginTop="0dip"
            android:layout_marginBottom="0dip"
            android:text="@string/FourSwords" />    
        <!-- Master Quest -->
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="0dip"
            android:layout_marginRight="0dip"
            android:layout_marginTop="0dip"
            android:layout_marginBottom="0dip"
            android:text="@string/MasterQuest" />    
        <!-- Wind Waker -->
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="0dip"
            android:layout_marginRight="0dip"
            android:layout_marginTop="0dip"
            android:layout_marginBottom="0dip"
            android:text="@string/WindWaker" 
            android:onClick= "WWMenu"/>    
        <!-- Four Swords Adventures -->
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="0dip"
            android:layout_marginRight="0dip"
            android:layout_marginTop="0dip"
            android:layout_marginBottom="0dip"
            android:text="@string/FourSwordsAdventures" />    
        <!-- Minish Cap -->
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="0dip"
            android:layout_marginRight="0dip"
            android:layout_marginTop="0dip"
            android:layout_marginBottom="0dip"
            android:text="@string/MinishCap" />    
        <!-- Twilight Princess -->
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="0dip"
            android:layout_marginRight="0dip"
            android:layout_marginTop="0dip"
            android:layout_marginBottom="0dip"
            android:text="@string/TwilightPrincess" />    
        <!-- Phantom Hourglass -->
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="0dip"
            android:layout_marginRight="0dip"
            android:layout_marginTop="0dip"
            android:layout_marginBottom="0dip"
            android:text="@string/PhantomHourglass" />
        <!-- Spirit Tracks -->
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="0dip"
            android:layout_marginRight="0dip"
            android:layout_marginTop="0dip"
            android:layout_marginBottom="0dip"
            android:text="@string/SpiritTracks" />
        <!-- Skyward Sword -->
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="0dip"
            android:layout_marginRight="0dip"
            android:layout_marginTop="0dip"
            android:layout_marginBottom="0dip"
            android:text="@string/SkywardSword" />
        <!-- Ocarina of Time 3D -->
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="0dip"
            android:layout_marginRight="0dip"
            android:layout_marginTop="0dip"
            android:layout_marginBottom="0dip"
            android:text="@string/OcarinaOfTime3D" />
        <!-- Wind Waker HD -->
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="0dip"
            android:layout_marginRight="0dip"
            android:layout_marginTop="0dip"
            android:layout_marginBottom="0dip"
            android:text="@string/WindWakerHD" />
        <!-- Link Between Worlds -->
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="0dip"
            android:layout_marginRight="0dip"
            android:layout_marginTop="0dip"
            android:layout_marginBottom="0dip"
            android:text="@string/LinkBetweenWorlds" />
    </LinearLayout> 
</ScrollView>

当我说它们相互重叠时,我的意思是:

【问题讨论】:

  • 为什么是-5dp?这就是造成重叠的原因。为什么不为零 dp?
  • @RichardLeMesurier 当我将它们设置为 0dip 时,它们会显示白色背景。我希望他们在不显示背景或重叠的情况下相互对接。我不知道是否有一种简单的方法可以做到这一点。 Example
  • When I set them to 0dip they have the white background showing..dint 让你...
  • @Lal 这就是我的意思。 Example
  • 检查您的前 2 个按钮是否有任何填充...

标签: android android-layout android-button


【解决方案1】:

搜索后,我发现了一些更简单但基本上仍然可以完成我想做的事情。我可以使用无边框按钮来代替调整按钮的大小。

在 .xml 文件中,我必须将这行代码添加到我的按钮中:

style="?android:attr/borderlessButtonStyle"

使我的一个按钮的完整代码如下所示:

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/Zelda"
    style="?android:attr/borderlessButtonStyle" />

我所要做的就是将 Manifest 中的 minSdkVersion 更改为 11。 (抱歉,任何想要运行此应用的旧手机用户。)

这是现在的样子:

【讨论】:

    【解决方案2】:

    您应该改用 ListView,然后设置处理每个选项的 onClick 函数。 这是一个很好的例子: Android - How to create clickable listview?

    【讨论】:

    • 我可以试试看。我不知道可以点击列表视图。
    • 如果您需要帮助,只需发表评论,当我回到家时,我会向您展示我使用 ListView 实现可点击的博客标题的博客阅读器应用程序,这将花费我几个小时,所以只需在此处发布我会尽我所能尽快给你。顺便说一句,为什么thumpbs down?
    • 如果我最终需要帮助,我会告诉你情况如何。我非常感谢您的帮助。就像我上面提到的,这是一个供个人使用的应用程序,只有在它变得更好时才会上市。我认为按钮是最好的选择,但可点击的列表视图是一种选择。至于不赞成的 idk,我不能用我目前的代表投票赞成或反对。我认为这是一个可行的解决方案。
    猜你喜欢
    • 1970-01-01
    • 2020-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多