【问题标题】:Mp3 player with messed up buttonsMp3 播放器的按钮弄乱了
【发布时间】:2014-08-05 16:45:55
【问题描述】:

我正在开发 MP3 播放器,但我刚刚意识到我的 main_activity.xml 搞砸了

我有 3 个按钮,上一个、播放/暂停和下一个,如图所示 here

我只有 1 个声望点,所以暂时不上传图片。

这是 XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:orientation="vertical"
    android:weightSum="7"
    tools:ignore="UselessParent" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_weight="6"
        android:orientation="vertical" >

        <ImageView
            android:id="@+id/bg"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:contentDescription="@string/Caratula" >
        </ImageView>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/buttons"
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:background="#FFFFFF"
        android:gravity="center"
        android:orientation="horizontal"
        android:weightSum="5" >

        <Button
            android:id="@+id/btnPrevious"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_margin="5dp"
            android:background="@drawable/btnprevious"
            android:onClick="click" />

        <View
            android:layout_width="0dip"
            android:layout_height="0dip" />

        <Button                          
            android:id="@+id/btnPlay"
            android:layout_width="0dip"
            android:layout_height="fill_parent"
            android:layout_margin="5dp"
            android:background="@drawable/play"
            android:onClick="click"  />



        <View
            android:layout_width="0dip"
            android:layout_height="0dip" />

        <Button
            android:id="@+id/btnNext"
            android:layout_width="0dip"
            android:layout_height="fill_parent"
            android:layout_margin="5dp"
            android:background="@drawable/next"
            android:onClick="click" />
    </LinearLayout>
</LinearLayout>

我会在这里感谢您的帮助。

【问题讨论】:

    标签: android xml eclipse button


    【解决方案1】:

    我不确定您希望如何布置按钮,但请尝试对包含按钮的 LinearLayout 进行此操作...

    <LinearLayout
        android:id="@+id/buttons"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#FFFFFF"
        android:gravity="center"
        android:orientation="horizontal" >
    
        <Button
            android:id="@+id/btnPrevious"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:layout_margin="5dp"
            android:background="@drawable/btnprevious"
            android:onClick="click" />
    
        <View
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1" />
    
        <Button                          
            android:id="@+id/btnPlay"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:layout_margin="5dp"
            android:background="@drawable/play"
            android:onClick="click"  />
    
        <View
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1" />
    
        <Button
            android:id="@+id/btnNext"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:layout_margin="5dp"
            android:background="@drawable/next"
            android:onClick="click" />
    </LinearLayout>
    

    您可以去掉 android:weightSum 属性 - 它是可选的,总重量总和将计算为 LinearLayout 中小部件的所有 android:layout_weight 属性的总和。使用上述方法,每个ButtonView 将被分配为LinearLayout 宽度的五分之一。

    【讨论】:

    • 我收到了一些“嵌套权重不利于性能”的警告。我应该删除导致此错误的行吗?
    • 没有。这只是 Android Lint 的迂腐。当然,嵌套太多(不仅是权重,还有嵌套布局)可能会影响性能,但这并不意味着它实际上会。测试一下,如果它可以正常工作。如果您可以整理并减少事情,那就这样做。
    猜你喜欢
    • 2016-05-29
    • 2010-11-08
    • 1970-01-01
    • 1970-01-01
    • 2017-12-14
    • 1970-01-01
    • 2014-07-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多