android - 2 列 TableLayout,每列准确占 50%

标签 android layout resources

自从我第一次接触 Android 以来,这个问题一直困扰着我。我不能使两列 TableLayout 中的两列各精确到 50%。

这是一个例子:

<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent" >

    <TableLayout
        android:id="@+id/tablelayout"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:paddingRight="2dip" >

        <TableRow>
            <TextView
                style="@style/TextViewStandard"
                android_layout_span="2"
                android:layout_weight="1" 
                android:text="Bla" />
        </TableRow>

        <TableRow>
            <TextView
                style="@style/TextViewStandard"
                android:layout_weight="1"
                android:text="Name:" />

            <EditText
                style="@style/EditTextStandard"
                android:id="@+id/et_name"
                android:layout_weight="1" />
        </TableRow>

        <TableRow>
            <TextView
                style="@style/TextViewStandard"
                android:layout_weight="1"
                android:text="URL:" />

            <EditText
                style="@style/EditTextStandard"
                android:id="@+id/et_url"
                android:layout_weight="1" />
        </TableRow>

        <TableRow>
            <Button
                style="@style/ButtonStandard"
                android:layout_column="0"
                android:layout_marginTop="6dip"
                android:onClick="onClickOk"
                android:text="@android:string/ok" />
        </TableRow>
    </TableLayout>
</ScrollView>

下面是相应的样式定义:

<resources>
    <style name="ButtonStandard" parent="@android:style/Widget.Button">
        <item name="android:layout_height">wrap_content</item>
        <item name="android:layout_width">fill_parent</item>
    </style>

    <style name="EditTextStandard" parent="@android:style/Widget.EditText">
        <item name="android:layout_height">wrap_content</item>
        <item name="android:layout_marginLeft">2dip</item>
        <item name="android:layout_marginRight">2dip</item>
        <item name="android:layout_marginTop">2dip</item>
        <item name="android:layout_width">fill_parent</item>
    </style>

    <style name="TextViewStandard" parent="@android:style/Widget.TextView">
        <item name="android:layout_height">wrap_content</item>
        <item name="android:layout_marginLeft">2dip</item>
        <item name="android:layout_marginRight">2dip</item>
        <item name="android:layout_marginTop">2dip</item>
        <item name="android:layout_width">fill_parent</item>
        <item name="android:textColor">@android:color/white</item>
    </style>
</resources>

这真的会因为涉及到 CheckBox 而变得一团糟。

我的定义有什么问题?

提前非常感谢。 哈杰维

最佳答案

您是否尝试过将 android:layout_width 属性设置为 0dp (当然将 android:layout_weight 保持为 1 ) 在 subview 上?

当我也想在两个 subview 之间平均分配给定空间时,我遇到过类似的情况,但由于具有“更广泛内容”的 subview 在其父 View 中也变得比其兄弟 View 更宽,因此未能这样做。这是因为较宽的 child 具有较大的初始宽度。确保两个 child 从相同的宽度开始(android:layout_width="0dp" 在他们两个上)也保证在他们之间平均分配空间。

关于android - 2 列 TableLayout,每列准确占 50%,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4725024/

相关文章:

android - 添加Android WorkManager时,ListenableFuture已经存在程序类型

android - 在钛中,是否可以从 android 模块访问 3rd-party-jar-bundled 资源文件( xml )?

javascript - d3.pack() : multiple parent circles each with a unique color

performance - 经典ASP-长时间运行的进程。为什么这样不好呢?

带有大文件的 GWT ExternalTextResource

Android拖放图片: ACTION_DROP never called

带有音乐流媒体的 Android 应用程序

android - 如何将相机闪光灯用作手电筒?

java - Swing GridBagLayout 可调整屏幕大小

ios - UICollectionView 中的固定项目距离