Android - 屏幕的布局宽度/高度百分比?

时间:2014-07-31 16:16:51

标签: android eclipse android-layout basic4android

我正在测试B4A(Basic4Android)并且有一个叫做“设计师脚本”的东西,这基本上允许你在屏幕上定位元素。 例如:
TextBox1.setTopAndBottom(0%y, 50%y)
TextBox1.setLeftAndRight(0%x, 50%x)

当这个脚本运行时,它会自动将TextBox1定位在屏幕上,想象屏幕是100像素乘100像素(100x100),TextBox1将放置在(0,0)(左上角),(50, 50)(右下)。

如何在Eclipse中实现这样的功能?
我无法弄清楚。我想定位一个元素(例如TextBox1)以适应25%的宽度和50%的高度。我该如何做到这一点?

3 个答案:

答案 0 :(得分:1)

行。这是技巧

简单来说:制作一个1px( px,而不是dp - 你不希望它缩放,但小到足以可转换!)TextView将是您的隐身(您将其保持透明并且不会在其中设置任何文字)“宇宙中心”。
然后拉伸另一个TextView,但限制它保持中心的(50%-1 / 2 px)和 (再次,50%) - 1/2 px)它:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <TextView
        android:id="@+id/centerPoint"
        android:layout_width="1px"
        android:layout_height="1px"
        android:layout_centerInParent="true"
    />
    <TextView
        android:id="@+id/myText"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_toLeftOf="@id/centerPoint"
        android:layout_above="@id/centerPoint"
    />
</RelativeLayout>

答案 1 :(得分:1)

考虑使用Android percent support library,其中添加了PercentRelativeLayoutPercentFrameLayout,允许孩子按百分比定义他们的大小。

e.g。

 <android.support.percent.PercentFrameLayout
         xmlns:android="http://schemas.android.com/apk/res/android"
         xmlns:app="http://schemas.android.com/apk/res-auto"
         android:layout_width="match_parent"
         android:layout_height="match_parent">
     <ImageView
         app:layout_widthPercent="50%"
         app:layout_heightPercent="50%"
         app:layout_marginTopPercent="25%"
         app:layout_marginLeftPercent="25%"/>
 </android.support.percent.PercentFrameLayout/>

答案 2 :(得分:-2)

您需要使用LinearLayout和权重。

http://developer.android.com/guide/topics/ui/layout/linear.html

6个TextView的示例,每行占据屏幕高度的50%,每行内每个TextView将占据屏幕宽度的33%。

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/text1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1" />

        <TextView
            android:id="@+id/text2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1" />

        <TextView
            android:id="@+id/text3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/text4"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1" />

        <TextView
            android:id="@+id/text5"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1" />

        <TextView
            android:id="@+id/text6"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1" />
    </LinearLayout>
</LinearLayout>
相关问题