Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 无法正确设置相对布局_Android_Android Layout - Fatal编程技术网

Android 无法正确设置相对布局

Android 无法正确设置相对布局,android,android-layout,Android,Android Layout,我一直在努力按照我想要的方式设置我的相对布局。我想在左边有一个编辑文本,中间有一个Twitter视图,右边有一个按钮,所有在ListVIEW上的按钮都是我能得到的…p> 以下是我正在使用的xml: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_pa

我一直在努力按照我想要的方式设置我的相对布局。我想在左边有一个编辑文本,中间有一个Twitter视图,右边有一个按钮,所有在ListVIEW上的按钮都是我能得到的…p> 以下是我正在使用的xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <EditText
        android:id="@+id/new_bookmark_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_toLeftOf="@+id/new_bookmark_clock"
        android:ems="10"
        android:inputType="text" />

    <TextView
        android:id="@+id/new_bookmark_clock"
        android:layout_width="12sp"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@+id/btn_add_new_bookmark"
        android:text="@string/xx_xx_xx" />

    <Button
        android:id="@+id/btn_add_new_bookmark"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="@string/add" />

     <ListView
         android:id="@+id/bookmark_list"
         android:layout_width="fill_parent"
         android:layout_height="fill_parent"
         android:layout_below="@+id/new_bookmark_name" />


</RelativeLayout>
有人能帮我吗?

试试这个

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<EditText
    android:id="@+id/new_bookmark_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_toLeftOf="@+id/new_bookmark_clock"
    android:layout_alignBaseline="@+id/btn_add_new_bookmark"
    android:ems="10"
    android:inputType="text" />

<TextView
    android:id="@+id/new_bookmark_clock"
    android:layout_width="wrap_content"
    android:layout_alignBaseline="@+id/btn_add_new_bookmark"
    android:layout_height="wrap_content"
    android:layout_toLeftOf="@+id/btn_add_new_bookmark"
    android:text="@string/xx_xx_xx" />

<Button
    android:id="@+id/btn_add_new_bookmark"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:text="@string/add" />

 <ListView
     android:id="@+id/bookmark_list"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:layout_below="@+id/new_bookmark_name" />


</RelativeLayout>

您可以这样尝试,这样可以使您的顶部工具栏与列表视图分开。对于textview,也不要使用dp。Sp用于文本大小

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

<RelativeLayout  android:layout_width="fill_parent"
                 android:layout_height="wrap_content"
                 android:id ="@+id/top_bar">

<EditText
    android:id="@+id/new_bookmark_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_toLeftOf="@+id/new_bookmark_clock"
    android:ems="10"
    android:inputType="text" />

<TextView
    android:id="@+id/new_bookmark_clock"
    android:layout_width="12sp"
    android:layout_height="wrap_content"
    android:layout_toLeftOf="@+id/btn_add_new_bookmark"
    android:text="@string/xx_xx_xx" />

<Button
    android:id="@+id/btn_add_new_bookmark"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:text="@string/add" />

 </RelativeLayout

 <ListView
     android:id="@+id/bookmark_list"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
  />
您可以使用以下选项:

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

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <EditText
        android:id="@+id/new_bookmark_name"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="0.5"
        android:ems="10"
        android:inputType="text" />

    <TextView
        android:id="@+id/new_bookmark_clock"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="0.3"
        android:text="XXXXXXXXX" />

    <Button
        android:id="@+id/btn_add_new_bookmark"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="0.2"
        android:text="Add" />
 </LinearLayout>

 <ListView
    android:id="@+id/bookmark_list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_below="@+id/new_bookmark_name" />

 </LinearLayout>

您可以通过线性布局更好地实现这一目标:

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

    <EditText
        android:id="@+id/new_bookmark_name"
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_toLeftOf="@+id/new_bookmark_clock"
        android:ems="10"
        android:inputType="text" />

    <TextView
        android:id="@+id/new_bookmark_clock"
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@+id/btn_add_new_bookmark"
        android:text="@string/xx_xx_xx" />

    <Button
        android:id="@+id/btn_add_new_bookmark"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="@string/add" />

</LinearLayout>
试试下面的 将android:layout\u width=12sp更改为android:layout\u width=wrap\u内容


您为Textview提供固定宽度的问题会产生问题,我刚刚添加了一个相对布局,实现了您的目标,请查看下面的代码

试试这个:

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <EditText
            android:id="@+id/new_bookmark_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10"
            android:inputType="text" />

        <TextView
            android:id="@+id/new_bookmark_clock"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/xx_xx_xx" />

        <Button
            android:id="@+id/btn_add_new_bookmark"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/add" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <ListView
            android:id="@+id/bookmark_list"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_below="@+id/new_bookmark_name" />
    </LinearLayout>

</LinearLayout>

`android:layout_width=12sp`?你想这样吗@RedCricket欢迎您,请接受答案,如果它符合您的问题,那么为了实现上图中给出的内容,我更愿意将前3个视图保持在单独的相对布局中,然后提供一个列表视图。由于我使用线性布局作为根布局,且方向垂直,因此listview始终位于顶部的_栏下方。如果你想使用相对布局作为根布局,你可以给listview android:layout_lower=@+id/top_条
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >


    <RelativeLayout 
        android:id="@+id/layout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">



    <EditText
        android:id="@+id/new_bookmark_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:ems="10"
        android:inputType="text" />

    <TextView
        android:id="@+id/new_bookmark_clock"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerInParent="true"
        android:layout_toRightOf="@+id/new_bookmark_name"
        android:gravity="center"
        android:text="xx_xx_xx" />

    <Button
        android:id="@+id/btn_add_new_bookmark"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_centerInParent=""
        android:text="add" />

    </RelativeLayout>

     <ListView
         android:id="@+id/bookmark_list"
         android:layout_width="fill_parent"
         android:layout_height="fill_parent"
         android:layout_below="@+id/layout" />


</RelativeLayout>
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <EditText
            android:id="@+id/new_bookmark_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10"
            android:inputType="text" />

        <TextView
            android:id="@+id/new_bookmark_clock"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/xx_xx_xx" />

        <Button
            android:id="@+id/btn_add_new_bookmark"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/add" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <ListView
            android:id="@+id/bookmark_list"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_below="@+id/new_bookmark_name" />
    </LinearLayout>

</LinearLayout>