Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/219.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,我一整天都在努力让它发挥作用,我想我可以使用RelativeLayout android:layout_weight="0.3" 将三个按钮放置在屏幕右侧,不居中,但从顶部向下30% 这可能吗?如果可能,我该怎么做 以下是我的XML,其中显示了三个按钮,它们相互下方,但位于右上角: </RelativeLayout> <LinearLayout android:id="@+id/rightRelativeLayout" a

我一整天都在努力让它发挥作用,我想我可以使用
RelativeLayout

android:layout_weight="0.3"  
将三个按钮放置在屏幕右侧,不居中,但从顶部向下30%

这可能吗?如果可能,我该怎么做

以下是我的XML,其中显示了三个按钮,它们相互下方,但位于右上角:

   </RelativeLayout>
      <LinearLayout
        android:id="@+id/rightRelativeLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_weight="1"
        android:orientation="vertical"
        >
            <ImageButton
                android:id="@+id/btn_A" 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="A"
                android:src="@drawable/drawer_1"
                android:background="@android:color/transparent"


            /> 
            <ImageButton
                android:id="@+id/btn_B" 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:layout_weight="1"
                android:text="B"
                android:src="@drawable/drawer_1"
                android:background="@android:color/transparent"

                android:layout_below="@+id/btn_A"
            />
            <ImageButton
                android:id="@+id/btn_C" 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:layout_marginTop="10dp"
                android:text="C"
                android:src="@drawable/drawer_1"
                android:background="@android:color/transparent"

                android:layout_below="@+id/btn_B"

            />
        </LinearLayout>

[更新]为有需要的人添加了最终工作版本

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/RelativeLayout1"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical">

    <Gallery xmlns:android="http://schemas.android.com/apk/res/android"
             android:id="@+id/examplegallery" 
             android:layout_width="fill_parent"
             android:layout_height="wrap_content" />
   <RelativeLayout 
        android:id="@+id/InnerRelativeLayout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" >
    <Button 
        android:id="@+id/btn_newpen_drawtext" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Pen"
    />      
    <EditText 
        android:id="@+id/etx_addtext_drawtext"
        android:layout_width="fill_parent"
        android:layout_toLeftOf="@+id/btn_delete_pen"
        android:layout_toRightOf="@+id/btn_newpen_drawtext"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:text=""
    />
        <Button 
            android:id="@+id/btn_delete_pen" 
            android:layout_toLeftOf="@+id/btn_save_drawtext"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Del"
        />
        <Button 
            android:id="@+id/btn_save_drawtext" 
            android:layout_alignParentRight="true" 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="save"
        />

   </RelativeLayout>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rightLinerLayoutL0"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_toRightOf="@+id/RelativeLayout1"
    android:weightSum="1.0"
    android:layout_alignParentRight="true"
    android:orientation="vertical"
    >
    <LinearLayout android:layout_height="0dp" 
                  android:id="@+id/rightLinerLayoutL1" 
                  android:layout_weight="0.15" 
                  android:layout_width="0dp">
    </LinearLayout>
    <LinearLayout android:layout_height="0dp" 
                  android:orientation="vertical" 
                  android:id="@+id/rightLinerLayoutL2" 
                  android:layout_weight="0.85" 
                  android:layout_width="wrap_content">

        <ImageButton android:text="Button_A" 
            android:background="@android:color/transparent" 
            android:layout_height="wrap_content" 
            android:id="@+id/btn_A" 
            android:layout_width="wrap_content" 
            android:layout_gravity="right" 
            android:src="@drawable/drawer"
            android:onClick="btnAListener">
        </ImageButton>
        <ImageButton android:text="Button_B" 
            android:background="@android:color/transparent" 
            android:layout_height="wrap_content" 
            android:layout_marginTop="10dp"
            android:id="@+id/btn_B" 
            android:layout_width="wrap_content" 
            android:layout_gravity="right" 
            android:src="@drawable/drawer"
            android:onClick="btnBListener">
        </ImageButton>
        <ImageButton android:text="Button_C" 
            android:background="@android:color/transparent" 
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:id="@+id/btn_C" 
            android:layout_width="wrap_content" 
            android:layout_gravity="right" 
            android:src="@drawable/drawer"
            android:onClick="btnCListener">
        </ImageButton>
    </LinearLayout>
</LinearLayout >

</RelativeLayout>

由于此xml文件没有给出错误,这并不意味着您可以将这些属性(与LinearLayout关联的权重、方向)与RelativeLayout一起使用。请先参阅相关文档了解RelativeLayout

将重量为0.3和0.7(高度为0dp)的2段线性布局(L1、L2)堆叠在垂直方向为1.0的线性布局(L0)内。将按钮置于0.7加权布局(L2)中。这样,按钮上方的间距为30%

然后,您可以将包含的线性布局(L0)放置在RelativeLayout(R0)中,并将L0放置在父对象(R0)的右侧,从而将整个对象放置在屏幕的右侧

编辑:我的版本最终与Milde几乎相同,只是使用了
RelativeLayout
的根,这样我就可以将
layout\u alignParentRight=“true”
放置在布局中,以确保其对齐。我发现的唯一症结是确保您的第一个线性布局的高度为
fill\u parent
,这样您的间隔实际上是屏幕的30%

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent">
    <LinearLayout android:orientation="vertical" android:layout_height="fill_parent" android:layout_width="wrap_content" android:weightSum="1.0" android:layout_alignParentRight="true">
        <LinearLayout android:layout_weight="0.3" android:layout_height="0dp" android:layout_width="0dp" />
        <LinearLayout android:layout_weight="0.7" android:layout_height="0dp" android:layout_width="wrap_content" android:orientation="vertical">
            <Button android:text="Button 1" android:layout_width="wrap_content" android:layout_height="wrap_content" />
            <Button android:text="Button 2" android:layout_width="wrap_content" android:layout_height="wrap_content" />
            <Button android:text="Button 3" android:layout_width="wrap_content" android:layout_height="wrap_content" />
        </LinearLayout>
    </LinearLayout>
</RelativeLayout>

试试这个:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rightLinerLayoutL0"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    >
    <LinearLayout android:id="@+id/rightLinerLayoutL1" android:layout_weight="0.3" android:layout_width="fill_parent">
    </LinearLayout>
    <LinearLayout android:orientation="vertical" android:id="@+id/rightLinerLayoutL2" android:layout_weight="0.7" android:layout_width="fill_parent">
        <Button android:text="Button_A" android:layout_height="wrap_content" android:id="@+id/btn_A" android:layout_width="wrap_content" android:layout_gravity="right"></Button>
        <Button android:text="Button_B" android:layout_height="wrap_content" android:id="@+id/btn_B" android:layout_width="wrap_content" android:layout_gravity="right"></Button>
        <Button android:text="Button_C" android:layout_height="wrap_content" android:id="@+id/btn_C" android:layout_width="wrap_content" android:layout_gravity="right"></Button>
    </LinearLayout>
</LinearLayout >


它实际上是一个线性布局,只是在一些测试之后忘记将其更改回去了。@markf我用您建议的代码实现更新了我的问题。当你有时间的时候,请评论。我认为某个地方有一个小错误,因为屏幕上没有显示任何内容screen@Erik请看我的答案(我没有使用相对布局),但请将markf标记为正确。。。我刚发了一些code@markf谢谢你的帮助。它工作得很好。我认为你的答案是正确的。下面你说:“(试着给每一个背景上色),最好用RelativeLayout把它冲洗干净。”。我有背景透明的png图像。什么是“同花顺”@Erik谢谢!“flush”的简单意思是“向上推”。关于彩色背景的评论只适用于当你试图使布局正确并且看不出为什么它不起作用的时候。它有助于查看宽度和重力设置是否正常。当你对布局满意时删除它们。@Milde你的代码工作得很好,我可以用布局的权重调整Y位置。谢谢again@Erik没问题,但正如我在上面的编辑中提到的,它似乎不是“真实的”30%/70%。它更像是20/80,因为在LL2中插入按钮时,LL1正在缩小。。。不知道为什么。@Milka无论如何,我认为它现在起作用了,所以我可以继续前进。将等待@markf的第二个意见。我没有一个更大的设备来测试这个,所以它必须工作上面更新的代码没有给我错误,也许尝试将它们设置为0px,而不是删除attributes@Milde尝试调整宽度,因为它们填充整个屏幕(尝试为每个屏幕的背景着色),最好使用RelativeLayout将其正确刷新,只在需要设置的东西上设置宽度。L1上的
fill\u parent
作为
0dp
会更好,L2上的
fill\u parent
作为
wrap\u content
会更好,否则您将我的描述完美地转换为示例:)我更新了我的答案,以填充我承诺的示例:)