简体   繁体   English

Android按钮的布局

[英]Layout of Android Buttons

I need a certain layout for an android app. 我需要某个Android应用程序的特定布局。 I have the custom buttons and am able to load them in to a button but need help on how to lay them out on the screen. 我有自定义按钮,我可以将它们加载到按钮,但需要帮助,如何将它们放在屏幕上。 I am a newbie to android development (See my screen name). 我是android开发的新手(请参阅我的屏幕名称)。 Trying to learn how to use .xml 尝试学习如何使用.xml

I have attached what I need and what xml code I already have. 我已经附加了我需要的东西以及我已经拥有的xml代码。 Any help would be greatly appreciated. 任何帮助将不胜感激。

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

她是布局

Try this.. 尝试这个..

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

It may helpful to you.. 它可能对你有帮助..

I see that you are trying to display images as buttons. 我看到你试图将图像显示为按钮。 Hence, I would recommend that you use ImageButton instead of a Button. 因此,我建议您使用ImageButton而不是Button。 I have included some code. 我已经包含了一些代码。 There are a few different ways you can go about trying to achieve what you intend to here. 您可以通过几种不同的方式尝试实现您的目标。 But, I have to chosen to solely use LinearLayout just to keep it simple. 但是,我必须选择仅使用LinearLayout来保持简单。 Deepa has used a RelativeLayout to achieve this and it is a very good solution indeed. Deepa使用了RelativeLayout来实现这一点,这确实是一个非常好的解决方案。 You should look into RelativeLayout as it might help you in the future while structuring layouts. 您应该研究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>   

In the above code I have used the default launch icons as the images. 在上面的代码中,我使用默认启动图标作为图像。 You must replace them as necessary with you corresponding button images. 必须使用相应的按钮图像替换它们。 Also, there are 4 important aspects here that you must understand. 此外,您必须了解4个重要方面。

First - I have used android:background="@null" to get rid of a frame that is attached to the ImageButton. 首先-我使用android:background =“ @ null”摆脱了附着在ImageButton上的框架。 Try to run the code without this line and you will understand what I am talking about 尝试在没有此行的情况下运行代码,您将了解我在说什么

Second - This line: android:src = "@drawable/ic_launcher" is used to declare the image that you display as the image. 第二-此行:android:src =“ @ drawable / ic_launcher”用于声明要显示为图像的图像。

Third - If you want some space between the buttons you could put in some padding between each of these elements. 第三-如果您想在按钮之间留出一些空间,可以在每个元素之间放置一些填充。 You could do so by using the 'android:padding= xxdp' 您可以使用'android:padding = xxdp'来实现

Fourth - If you want the buttons to have a specific size, you could do so by replacing the values 'wrap_content' by 'xxdp' for the width and height. 第四 - 如果您希望按钮具有特定大小,您可以通过将'wrap_content'替换为'xxdp'作为宽度和高度来实现。 However, it is important that you add the following line when you intend to do so: android:scaleType = "fitXY". 但是,在您打算执行此操作时添加以下行非常重要:android:scaleType =“fitXY”。 This will tell the element that you want it to be laid out in an absolute manner. 这将告诉元素您希望以绝对方式布局它。 But, instead of resorting to this approach, I recommend that you resize the images appropriately before getting them into your layout. 但是,我建议您在将图像放入布局之前适当调整图像大小,而不是采用这种方法。

To populate this on the screen you need to set this as a view of certain activity. 要在屏幕上填充此内容,您需要将其设置为某个活动的视图。 for that you can refer below code. 为此,您可以参考以下代码。

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