【问题标题】:How do I place an object on the other side of the screen如何将对象放置在屏幕的另一侧
【发布时间】:2014-02-13 11:52:22
【问题描述】:

我正在为我正在学习的移动应用开发课程编写程序。该应用程序涉及我在屏幕上放置 2 个按钮。我知道当我输入 /2 时,它会将其放在中间,而当我输入 /3 时,它会将按钮放置在距离屏幕左侧三分之一处。因为我需要两个按钮,所以我想把左边的三分之一(我知道怎么做),右边的三分之一(我不知道怎么做)。我应该怎么做才能做到这一点?

我的代码 sn-p:

myRedButton.x = display.contentWidth /3 
myRedButton.y = display.contentHeight -50 
myGreenButton.x = display.contentWidth /2 
myGreenButton.y = display.contentHeight -100

我是移动应用编程领域的新手,所以请保持简单。谢谢!

【问题讨论】:

  • 谨慎选择布局并调整重力会有所帮助。
  • Idek “重力”指的是什么。我有很多东西要学
  • 确保您在 XML 文件中使用的是 RelativeLayout 而不是 LinearLayout。
  • Please close your question by clicking the checkmark to the left of the answer that helped you most
  • 您是否使用 corona 进行编码?然后你可以这样做:object.x = display.contentWidth-(display.contentWidth/3)。这会将对象放置到距屏幕右侧的间隙 width/3 的 x 位置。

标签: android mobile lua coronasdk


【解决方案1】:

我使用重量将屏幕分成 3 个部分,然后在其中放置按钮,检查它是否有效。

<LinearLayout
    android:id="@+id/lnrLeftContent"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="0.33"
    android:gravity="center"
    android:orientation="vertical" >

    <Button
        android:id="@+id/btnLeft"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

<LinearLayout
    android:id="@+id/lnrCenterContent"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="0.33"
    android:gravity="center"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/btnLeft"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

<LinearLayout
    android:id="@+id/lnrRightContent"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="0.33"
    android:gravity="center"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/btnLeft"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

【讨论】:

  • 不要使用三个LinearLayout 来完成此操作。可以由一个LinearLayout 完成。这将使视图更具递归性,您应该避免它。
  • 这是一个建议,具体要求由您决定。
  • 我只是建议你不要让你的Layout 嵌套请看看这个线程。 Stackoverflow: Caused by nested views?
  • 是的,我明白,android 这么说。我使用它是因为我要求我的布局是这样的,但是答案有帮助吗?
  • 我没有问这个问题 ;-) 所以不知道尝试了什么操作我刚刚回答了这个问题并看到了你的答案,所以我只是通过查看你的代码来发表评论。
【解决方案2】:

这里我放了三个TextView,你可以随意把它改成按钮。这会将三个视图排成一行,在屏幕上占据相同的空间。

<LinearLayout android:id="@+id/total_row_holder"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="3dp"
    android:orientation="horizontal" >

    <TextView android:id="@+id/total_textview"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="some other"
        android:padding="2dp"
        android:layout_gravity="center"
        android:background="#e8f4f9"
         />

    <TextView android:id="@+id/total_value_textview"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_marginLeft="1dp"
        android:text="you know"
        android:padding="2dp"

        android:background="@android:color/white" />

    <TextView android:id="@+id/net_value_textview"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="1dp"
        android:layout_weight="1"
        android:text="some value"
        android:padding="2dp"

        android:background="@android:color/white" />


</LinearLayout>

【讨论】:

    【解决方案3】:

    试试这个:

    local myGreenButton = display.newRect(0,0,50,50)
    myGreenButton.x = display.contentWidth- (display.contentWidth/3)
    myGreenButton.y = 100
    

    或者简单地说,

    local myGreenButton = display.newRect(0,0,50,50)
    myGreenButton.x = (2/3)*display.contentWidth
    myGreenButton.y = 200
    

    继续编码........ :)

    【讨论】:

    • @user1641187:如果您认为此答案有用,请将答案标记为已接受,否则该问题将保持为未回答。以后也请记住这一点。谢谢。继续编码..... :)
    猜你喜欢
    • 2023-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-06
    相关资源
    最近更新 更多