【问题标题】:Get TextView/Layout width according to device.根据设备获取 TextView/Layout 宽度。
【发布时间】:2014-06-29 11:14:44
【问题描述】:

我正在制作一个应用程序,首先我需要获得TextView 的大小,然后通过根据TextView 调整它来将其应用于Layout。但是尺寸会因设备而异……是否有任何程序可以解决这些差异。

【问题讨论】:

  • 最好避免对 UI 元素进行幼稚的计算。对布局的宽度使用“wrap_content”怎么样?
  • 我需要根据我的 textview 来控制布局的宽度。 bcz 我需要为我的布局添加背景。当我使用 wrap_content 时,它不会限制我的布局。
  • 我不明白你... wrap_content 包围了包含的视图以匹配它们的大小(除非你没有边距和/或填充)。
  • Thnx,明白了。尝试。

标签: android android-layout textview


【解决方案1】:

如果您希望您的项目在设备之间保持相同,我会将您的 textview 放置在线性布局中的空视图之间,而不是使用 sum-weight 并将布局权重调整为您想要的。然后将 layout_height 或 width (取决于水平与垂直线性布局)调整到适当的量。例如,水平线性布局您可以将 layout_width 设置为 0dp,垂直您可以将 layout_height 设置为 0dp。这是来自旧项目的一些示例代码

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:weightSum="4" >

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

        <Button
        android:id="@+id/add_new_customer_button"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="2"
        android:text="@string/add_customer_button_label"/>

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

</LinearLayout>

这会使某些东西看起来像

[空视图][屏幕居中 1/2 的按钮][空视图]

【讨论】:

    猜你喜欢
    • 2011-09-03
    • 1970-01-01
    • 1970-01-01
    • 2012-12-25
    • 1970-01-01
    • 2014-08-12
    • 1970-01-01
    • 2011-10-14
    • 1970-01-01
    相关资源
    最近更新 更多