【问题标题】:Multiple Buttons/Objects on the same row同一行上的多个按钮/对象
【发布时间】:2012-06-08 23:27:32
【问题描述】:

我希望Buttons 在同一行中彼此相邻,并且我希望在EditText 旁边有一个Button

我有 Eclipse 插件,所以我有图形布局和文本。我无法发布图片,但我会对其进行描述:它有两个TextViews(不相关),然后是一个EditText,然后在下一行有一个小的未标记Button。接下来的四行是标记为 Turn N、Turn E、Turn S 和 Turn W 的按钮。

所以,在 EditText 旁边是我希望无标题的 Button 的位置,并且我希望以类似指南针的方式设置 N、E、S、W Buttons。但是,当我尝试在 GUI 上拖动它们时,它会将它们绑定到行,并且每当我尝试更改布局时,它会影响所有内容,而不仅仅是我右键单击的一个对象。

我该如何解决这个问题?

【问题讨论】:

    标签: android android-layout positioning android-button


    【解决方案1】:

    类似这样的:

      <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/relativeLayout1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:text="TextView" />
    
        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@id/textView1"
            android:text="TextView" />
    
        <EditText
            android:id="@+id/editText1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@id/textView2"
            android:layout_toLeftOf="@+id/button1" >
        </EditText>
    
        <Button
            android:id="@id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_below="@id/textView2"
            android:text="Button" />
    
        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/editText1"
            android:layout_centerHorizontal="true"
            android:text="Button" />
    
        <Button
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/button2"
            android:layout_toLeftOf="@id/button2"
            android:text="Button" />
    
        <Button
            android:id="@+id/button4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/button2"
            android:layout_toRightOf="@id/button2"
            android:text="Button" />
    
        <Button
            android:id="@+id/button5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/button4"
            android:layout_toLeftOf="@id/button4"
            android:text="Button" />
    
    </RelativeLayout>
    

    【讨论】:

    • 谢谢,这很有帮助,现在我的 XML 图形布局视图可以按照我想要的方式显示它。 (我还添加了一些按钮),但是,现在在我的 Java 文件中,所有引用 main.js 的东西都有错误。在“setContentView(R.layout.main);”下它说错误:“主要无法解析或不是字段。”我的 xml 文件没有显示任何错误,并且我尝试重新启动 eclipse,但我找不到任何问题。有什么建议么?谢谢!
    • @Zeldarulah 尝试清理你的项目(在菜单中Project-&gt; Clean)并检查你是否从你的包中导入了正确的R类(使用CTRL + SHIFT + O)(而不是import android.R;) .
    • 我确实有“import android.R;”,但我尝试了 Project -> Clean 和 CTRL + Shift + O,但它并没有改变它。应该“导入android.R;”有什么不同吗?再次感谢。
    • @Zeldarulah 那行import android.R; 隐藏了您的项目生成的R 类并且android 找不到布局,ids。删除那行,你应该没问题。 (如果您使用 android.R 命名空间的某些部分,则不要导入 import android.R;,只需在需要的地方使用 android.R.something
    • 好吧,解决了!万分感谢。我不太确定为什么该导入会导致问题,但非常感谢。如果我能支持你的答案,我会的,但我的代表仍然太低。
    【解决方案2】:

    如果没有 xml,我真的不能说它,但是如果你想以类似 compas 的方式定位按钮,你应该尝试创建方形相对布局,将按钮放置在上面,然后将按钮定位在顶部,左侧,父级的右下角

    【讨论】:

      猜你喜欢
      • 2016-09-23
      • 2017-11-28
      • 2019-02-17
      • 2018-04-17
      • 1970-01-01
      • 1970-01-01
      • 2019-07-10
      • 1970-01-01
      • 2012-11-03
      相关资源
      最近更新 更多