【问题标题】:Layout of Android ButtonsAndroid 按钮的布局
【发布时间】:2011-12-17 17:22:06
【问题描述】:

我需要一个 Android 应用程序的特定布局。我有自定义按钮,并且能够将它们加载到按钮中,但需要有关如何在屏幕上布置它们的帮助。我是 android 开发的新手(见我的网名)。尝试学习如何使用.xml

我已经附上了我需要的东西和我已经拥有的 xml 代码。任何帮助将不胜感激。

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent" 
              android:layout_height="fill_parent" 
              android:orientation="vertical" android:weightSum="1">


              <LinearLayout 
              android:layout_width="fill_parent" 
              android:layout_height="fill_parent" 
              android:orientation="horizontal" android:weightSum="1"
              android:gravity="center_vertical|center_horizontal" >
                  <Button android:gravity="center_vertical|center_horizontal" android:id="@+id/button" android:layout_height="wrap_content" android:padding="10dp" android:background="@drawable/schedule_button" android:layout_width="wrap_content"></Button>
                    <Button android:id="@+id/button" android:layout_height="wrap_content" android:padding="10dp" android:background="@drawable/grad_button" android:layout_width="wrap_content"></Button>
              </LinearLayout>


              <LinearLayout 
              android:layout_width="fill_parent" 
              android:layout_height="fill_parent" 
              android:orientation="horizontal" android:weightSum="1"
              android:gravity="center_vertical|center_horizontal" >
                 <Button android:gravity="center_vertical|center_horizontal" android:id="@+id/button" android:layout_height="wrap_content" android:padding="10dp" android:background="@drawable/schedule_button" android:layout_width="wrap_content"></Button>
                <Button android:id="@+id/button" android:layout_height="wrap_content" android:padding="10dp" android:background="@drawable/grad_button" android:layout_width="wrap_content"></Button>
              </LinearLayout>



</LinearLayout>

【问题讨论】:

    标签: java android xml button


    【解决方案1】:

    我看到您正在尝试将图像显示为按钮。因此,我建议您使用 ImageButton 而不是 Button。我已经包含了一些代码。您可以通过几种不同的方式来尝试实现您在此处的目标。但是,为了简单起见,我必须选择单独使用 LinearLayout。 Deepa 使用了 RelativeLayout 来实现这一点,这确实是一个非常好的解决方案。您应该研究一下 RelativeLayout,因为它可能会在您将来构建布局时帮助您。

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:orientation="vertical" >
    
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
        <ImageButton
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src = "@drawable/ic_launcher"
            android:background="@null"/>
        <ImageButton
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src = "@drawable/ic_launcher"
            android:background="@null"/>
    </LinearLayout>
    
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >    
        <ImageButton
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:src = "@drawable/ic_launcher"
            android:background="@null"
            android:layout_height="wrap_content"/>
    
        <ImageButton
            android:id="@+id/button4"
            android:layout_width="wrap_content"
            android:src = "@drawable/ic_launcher"
            android:background="@null"
            android:layout_height="wrap_content"/>
    </LinearLayout>
    
    
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >    
    
        <ImageButton
            android:id="@+id/button5"
            android:layout_width="wrap_content"
            android:src = "@drawable/ic_launcher"
            android:layout_height="wrap_content"
            android:background="@null"/>
    
        <ImageButton
            android:id="@+id/button6"
            android:layout_width="wrap_content"
            android:src = "@drawable/ic_launcher"
            android:layout_height="wrap_content"
            android:background="@null"/>
     </LinearLayout>   
    

    在上面的代码中,我使用了默认的启动图标作为图像。您必须根据需要将它们替换为相应的按钮图像。此外,您必须了解这里有 4 个重要方面。

    首先 - 我使用了 android:background="@null" 来摆脱附加到 ImageButton 的框架。尝试运行没有这一行的代码,你就会明白我在说什么

    第二 - 此行:android:src = "@drawable/ic_launcher" 用于声明您显示为图像的图像。

    第三 - 如果您希望按钮之间有一些空间,您可以在每个元素之间放置一些填充。您可以使用 'android:padding= xxdp'

    第四 - 如果您希望按钮具有特定大小,您可以通过将宽度和高度的值 'wrap_content' 替换为 'xxdp' 来实现。但是,当您打算这样做时添加以下行很重要:android:scaleType = "fitXY"。这将告诉元素您希望它以绝对方式布局。但是,我建议您在将图像放入布局之前适当地调整图像大小,而不是采用这种方法。

    【讨论】:

    • 谢谢,这正是我所需要的。感谢您非常详细的解释。
    【解决方案2】:

    试试这个..

    <?xml version="1.0" encoding="utf-8"?>
     <RelativeLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_gravity="center_vertical|center_horizontal">
     <Button 
      android:id="@+id/Button1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Schedules"/>
    
     <Button 
      android:id="@+id/Button2"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
     android:layout_toRightOf="@id/Button1"
     android:text="Grand Nite"
     />
    
    <Button
     android:id="@+id/Button3"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_below="@id/Button1"
     android:text="The paper"/>
    <Button
     android:id="@+id/Button4"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_toRightOf="@id/Button3"
     android:layout_below="@id/Button1"
     android:text="The Splash"
     />  
    
    <Button
     android:id="@+id/Button5"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_below="@id/Button3"
     android:text="Facebook"/>
     <Button
     android:id="@+id/Button6"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_toRightOf="@id/Button5"
     android:layout_below="@id/Button3"
     android:text="Twitter"
     />   
    </RelativeLayout>
    

    它可能对你有帮助..

    【讨论】:

      【解决方案3】:

      要在屏幕上填充此内容,您需要将其设置为特定活动的视图。为此,您可以参考下面的代码。

      public class ButtonLayout extends Activity {
          /** Called when the activity is first created. */
          @Override
          public void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              // You can put your XML file name here
              setContentView(R.layout.main);
      }
      }
      

      【讨论】:

      • 我的 java 类没有问题,而是 xml 文件中的布局。关于如何设置 xml 文件的任何帮助????
      • 设置 XML 手段..您想像控制一样进行拖放以方便开发?
      猜你喜欢
      • 1970-01-01
      • 2013-03-09
      • 1970-01-01
      • 2015-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多