iOS 到 Android:如何显示 TextView

发布于 2024-11-29 20:24:13 字数 271 浏览 1 评论 0原文

我有 iPhone 开发背景,这可能是我对 Android 感到困惑的原因,因为我对 Android 开发还很陌生。

我的问题是:如何创建两个 TextView,并指定它们在屏幕上的确切位置?例如,在 iPhone 上,我将创建一个 UILabel,生成一个指定标签大小和位置的矩形框架,然后将该矩形设置为 UILabel 的框架属性。

如果您能帮助我理解 Objective C 和 iOS 的 UILabel 的相似之处,那将是最有帮助的。

I have a background in iPhone development, which may be a cause of some of my confusion with Android, which I am very new at developing.

My question is this: How to I create two TextViews, and specify their exact location on screen? For example, on the iPhone, I would create a UILabel, generate a rectangular frame that specified the label's size and position, and then set this rectangle to the frame property of the UILabel.

If you can help me understand the similarities with Objective C and iOS' UILabel, that would be most helpful.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

微暖i 2024-12-06 20:24:13

在 Android 上,我们不使用绝对屏幕位置。这是非常不鼓励的。如果你来自 iOS,你这样想是很可以理解的。但你需要改变你的习惯。

我们不使用绝对位置,而是使用布局,例如 LinearLayoutRelativeLayoutFrameLayout。所有这些都允许您动态地安排您的视图。在许多情况下,它会自动适应屏幕尺寸,而屏幕尺寸因设备而异。

事实上,动态布局并没有什么奇怪的。许多主要的 UI 工具包(例如 GTK 或 Qt)的工作原理类似。在我看来,像素位置是一个坏主意,也许除了在苹果世界中,操作系统和硬件紧密耦合,但这实际上是一个例外。

因此,就您的情况而言,您所需要做的就是将文本视图放入适当的布局中。请阅读有关上述不同类型布局的文档和教程,以决定哪一种最好。问题是您希望如何将您的观点相对放置。

On Android, we don't use absolute screen positions. This is highly discouraged. It's pretty understandable that you think this way if you are coming from iOS. But you need to revise your habits.

Instead of absolute positions, we use layouts, such as LinearLayout, RelativeLayout or FrameLayout. All of these allow you to arrange your views dynamically. And in many cases, it will automagically adapt to the screen size, which vary a lot from device to device.

Actually, there's nothing exotic about dynamic layouts. Many major UI toolkits, such as GTK, or Qt, work similarly. Pixel position are a bad idea in my opinion, except maybe in the Apple world, where the OS and the hardware are tightly coupled, but this is an exception actually.

So, in your case, all that you need is to put your text views into the appropriate layout. Please read the documentation and tutorials about the different types of layouts mentioned above to decide which one is best. The question is how you want your views to be placed relatively to each other.

み青杉依旧 2024-12-06 20:24:13

在 eclipse 中创建一个基本的 Android 项目。您的项目中将有一个 main.xml 布局文件。您可以使用 Ctrl+Shift+r 在 Eclipse 中打开它,并键入 main.xml,

在清除其内容后将其复制粘贴到您的 xml 中。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:text="TextView One"
        android:layout_height="fill_parent"
        android:layout_weight="1"></TextView>
    <TextView
        android:id="@+id/textView2"
        android:layout_width="fill_parent"
        android:text="TextView Two"
        android:layout_height="fill_parent"
        android:layout_weight="1"></TextView>
</LinearLayout>

Create a basic Android project in eclipse. You will be having a main.xml layout file in your project. You can open it in Eclipse using Ctrl+Shift+r and keying in main.xml

copy paste this in your xml after clearing its content.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:text="TextView One"
        android:layout_height="fill_parent"
        android:layout_weight="1"></TextView>
    <TextView
        android:id="@+id/textView2"
        android:layout_width="fill_parent"
        android:text="TextView Two"
        android:layout_height="fill_parent"
        android:layout_weight="1"></TextView>
</LinearLayout>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文