【问题标题】:aligning in relative view, in xml android在xml android中在相对视图中对齐
【发布时间】:2011-11-30 20:43:56
【问题描述】:

我在屏幕上使用RelativeLayout。我将 RadioButtons 停靠在屏幕的右侧,我想将 TextViews 对齐到 RadioButtons 的左侧。

所以这里是我的文本视图的一个例子:

    <TextView
        android:id="@+id/editText2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@+id/radioButton1"
        android:gravity="right"
        android:hint="@string/error1" />

我不确定它是否将它们对齐到 RadioButton 的中心而不是下方,因为这对我不起作用。我讨厌 Eclipse 的 (adt) xml gui,并在 xml 文件中进行编辑,因为我没有得到我想要的东西。有更简单的gui、xml编辑器吗?

【问题讨论】:

    标签: android xml android-relativelayout


    【解决方案1】:

    好吧,目前没有更好的 xml gui 编辑器适用于 android。 IntelliJ Idea 10.5 甚至没有屏幕预览只有 xml 编辑器。

    现在使用屏幕 gui 编辑器和 xml 编辑器的技巧。您首先通过 ADT 附带的屏幕编辑器进行基本的 gui 编辑,然后使用属性或原始 xml 编辑器进行微调。

    根据您对上述问题的要求。这是解决方案:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    
    <RadioButton
        android:id="@+id/radioButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true" />
    
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_toLeftOf="@+id/radioButton"
        android:text="TextView" />
    
    </RelativeLayout>
    

    为了创建这个,我自己没有编辑 xml 文件。我只是使用了 ADT 附带的 gui 屏幕编辑器。以下是步骤

    • 我通过向导创建了具有相对布局的 xml,该向导也是 ADT 的一部分。
    • 从左窗格添加RadioButton 并将其拖到屏幕的右边缘,直到我看到两个绿色锚点出现,并且在黄色信息中出现说明对齐属性。
    • 然后我添加了TextView 并向左拖动直到看到绿色的锚点。
    • 然后我将TextView 的右边缘拖向RadioButton,直到我看到绿色锚点和黄色信息框中的正确layout_toLeftOf 属性。

    【讨论】:

    • 是不是太乱了。在其他设备上进行测试时,比例会发生变化..向下拖动它们也很麻烦..我不能总是将它们放在单选按钮的中心附近..那里没有 alignCenterOF
    • 它可以在不同的屏幕尺寸上正常工作。如您所见,上面没有提到硬编码的尺寸。因为我不知道你的最终布局是什么。以上仅锚定到父顶部。您也可以锚定到父底部,以使其在垂直方向上更加集中。您可以使用gravity 将内容与小部件/视图中的内容对齐。为了对齐视图本身,有layout_centerInParentlayout_centerHorizontallayout_centerVertical
    • 我想我会在里面放一个表格布局..然后放两行..这将不得不对齐它们......你怎么看
    • 您是使用此布局作为 ListView 的列表项的组件还是活动的布局?你的布局到底应该是什么?
    【解决方案2】:

    你不想用 toLeftOf radioButton1 代替 radioGroup 吗?

    另外,将 layout_width 更改为 wrap_content 而不是 fill_parent。

    【讨论】:

    • true..sorry..它会把旁边的文本视图居中吗?
    猜你喜欢
    • 2020-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-23
    • 1970-01-01
    相关资源
    最近更新 更多