繁体   English   中英

Android按钮的布局

[英]Layout of Android Buttons

我需要某个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>

她是布局

尝试这个..

<?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>

它可能对你有帮助..

我看到你试图将图像显示为按钮。 因此,我建议您使用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”。 这将告诉元素您希望以绝对方式布局它。 但是,我建议您在将图像放入布局之前适当调整图像大小,而不是采用这种方法。

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

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);
}
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM