简体   繁体   English

xml布局问题:不适用于所有屏幕

[英]xml Layout issue: doesn't fit in all screens

I am having an issue with the layout.I need to add the edittext with imagebutton button in the single row. 我在布局上遇到问题。我需要在单行中添加带有imagebutton按钮的edittext。

I need to add the 2 timepicker one for date.another for time. 我需要将2个时间选择器添加为date.another作为时间。

I get the two picker for date and time in a single row.But while clicking the preview screens in graphical layout,it shows overlapping and more spaces left between edittext and imagebutton for all screens. 我在一行中获得了两个日期和时间选择器。但是,在单击图形布局的预览屏幕时,它显示了重叠,并且所有屏幕的edittext和imagebutton之间都留有更多空间。

Below I am posted the codes what I have tried so far. 在下面,我张贴了到目前为止我尝试过的代码。

activity_main.xml: activity_main.xml中:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:onClick="selectDate" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="55dp"
        android:background="@drawable/text"
        android:ems="5"
        android:inputType="date" />

    <ImageButton
        android:id="@+id/imageButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/editText1"
        android:layout_toRightOf="@+id/editText1"
        android:contentDescription="@string/selectdate"
        android:cropToPadding="true"
        android:onClick="selectDate"
        android:src="@drawable/ic_datepicker" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:layout_alignBottom="@+id/imageButton1"
        android:layout_toRightOf="@+id/imageButton1"
        android:background="@drawable/text"
        android:ems="5"
        android:inputType="date" >

        <requestFocus />
    </EditText>

    <ImageButton
        android:id="@+id/imageButton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/editText2"
        android:layout_toRightOf="@+id/editText2"
        android:contentDescription="@string/selecttime"
        android:cropToPadding="true"
        android:onClick="selecttime"
        android:src="@drawable/ic_datepicker" />

</RelativeLayout>

Editext and imagebutton have to be in single row.It have to exactly fit for all devices.Anyone can help me with this.Thank you. Editext和imagebutton必须在同一行中,它必须完全适合所有设备。任何人都可以帮助我。谢谢。

Unfortunately, Android suffers from what is called "fragmentation problem". 不幸的是,Android遭受了所谓的“碎片化问题”。 One part of this problem is that you as a developer need to do some work to support different screen sizes. 这个问题的一部分是作为开发人员,您需要做一些工作来支持不同的屏幕尺寸。

Here is a guide that explains this process. 是解释此过程的指南。 You need to create different layouts for the different screen sizes that your app needs to support. 您需要为应用程序需要支持的不同屏幕尺寸创建不同的布局。

You could use a linear layout instead of a relative layout with the value orientation="horizontal". 您可以使用线性布局,而不要使用取向为“ horizo​​ntal”的相对布局。

You can find out more information here http://developer.android.com/guide/topics/ui/layout/linear.html but I've included a brief description below. 您可以在http://developer.android.com/guide/topics/ui/layout/linear.html上找到更多信息,但是我在下面进行了简要说明。

This will layout the edit text and image button horizontally automatically. 这将自动水平排列编辑文本和图像按钮。 To force them to appear on the same row and not overlap, you can assign each views width to be 0dp and their weights to be 1. 要强制它们显示在同一行上而不重叠,可以将每个视图的宽度分配为0dp,将其权重分配为1。

This will tell android to layout the views with equal width across the screen regardless of screen size. 这将告诉android在屏幕上以相等的宽度布局视图,而不管屏幕尺寸如何。 If you want one view to be wider, change its weight value so it is higher than the other. 如果要使一个视图变宽,请更改其权重值,使其高于另一个视图。 It always ends up being a ratio of the total weights. 它总是以总重量的比率结束。

You can try this layout, it will meet your requirement and fits in all screen. 您可以尝试这种布局,它将满足您的要求并适合所有屏幕。

Though i made some changes in your xml layout by replacing your Relative layout with linear layout 虽然我通过将线性布局替换为相对布局来对XML布局进行了一些更改

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:onClick="selectDate" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:layout_weight="1.5"
        android:inputType="date" />

    <ImageButton
        android:id="@+id/imageButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:cropToPadding="true"
        android:onClick="selectDate"
        android:src="@drawable/ic_launcher" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1.5"
        android:layout_gravity="bottom"
        android:inputType="date" >

    </EditText>

    <ImageButton
        android:id="@+id/imageButton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:cropToPadding="true"
        android:onClick="selecttime"
        android:src="@drawable/ic_launcher" />

</LinearLayout>

Hope this helps you somehow :) 希望这可以对您有所帮助:)

Put image text in one linearlayout which has orientation as "horizontal" and place edittext and button in that. 将图像文本放置在一个线性布局中,其方向为“水平”,然后在其中放置edittext和按钮。 You can use weightSum to give weights to the elements in the linear layout so that they will be at a distance which is same in proportion irrespective of device screen size. 您可以使用weightSum为线性布局中的元素赋予权重,以使它们之间的距离比例相同,而与设备屏幕的大小无关。

<LinearLayout
   android:id="@+id/linearlayout1"
   android:layout_width="match_parent"
   android:layout_height="30dp"
   android:orientation="horizontal"
   android:weightSum="2"
>
<EditText
        android:id="@+id/editText1"
        android:layout_width="0dp"
        android:layout_height="30dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="55dp"
        android:background="@drawable/text"
        android:ems="5"
        android:layout_weight="1.0"
        android:inputType="date" />

    <ImageButton
        android:id="@+id/imageButton1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/editText1"
        android:layout_toRightOf="@+id/editText1"
        android:contentDescription="@string/selectdate"
        android:cropToPadding="true"
        android:onClick="selectDate"
        android:layout_weight="1.0"
        android:src="@drawable/ic_datepicker" />
</LinearLayout>

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

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