Android 以编程方式在相对布局中定位按钮

Android 以编程方式在相对布局中定位按钮,android,android-layout,button,android-widget,android-button,Android,Android Layout,Button,Android Widget,Android Button,这是我正在使用的布局XML <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@color/tabTransparent" android:id="@+id/aLayoutRela

这是我正在使用的布局XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/tabTransparent"
    android:id="@+id/aLayoutRelative">

    <LinearLayout
        android:id="@+id/linearLayout8"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/linearLayout7"
        android:layout_below="@+id/linearLayout7"
        android:layout_marginTop="14dp" >

        <Button
            android:id="@+id/button5"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/text_accounts_aHistoryNotes" />
    </LinearLayout>

</RelativeLayout>
但在视图中,按钮是垂直的,上面没有文本;朝向页面左侧对齐(实际上朝向相对布局)。XML版本运行良好。我认为方向设置是罪魁祸首。在谷歌上搜索过各种各样的文档几乎帮不上什么忙

有什么想法吗


提前谢谢

你想达到什么目的

对于开始,请卸下

layoutParams.addRule(RelativeLayout.LEFT_OF, lastLinearLayout.getId());
所以你至少可以看到下面的按钮


为什么要将按钮放在线性布局中?

这行不是
layoutParams.addRule(RelativeLayout.LEFT_OF,lastLinearLayout.getId())使您的视图脱离屏幕?如果没有那行代码,我现在可以看到按钮。这些xml是在eclipse中使用组件设计自动生成的。看到按钮后,我想将按钮的样式设置为小。如何设置按钮以适应文本大小(小于整个屏幕宽度),请使用LayoutParams.WRAP_CONTENT而不是LayoutParams.FILL_PARENT:这就是设计规范。我所说的小按钮是指它必须将样式设置为android.R.attr.buttonStyleSmall。我现在得到了答案。我也可以使用按钮对象构造函数。我还有一个问题要问你。如何使不同屏幕密度的设备中的布局看起来相同?我读到有几点需要注意,比如使用WRAP_内容而不是FILL_父项,使用dp而不是px。然而,即使如此,我还是在较小的屏幕设备中看到一些对齐泄漏。有什么想法吗?
layoutParams.addRule(RelativeLayout.LEFT_OF, lastLinearLayout.getId());