iOS to Android: How to display a TextView

狂风中的少年 提交于 2020-01-06 03:41:17

问题


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.


回答1:


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>



回答2:


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.



来源:https://stackoverflow.com/questions/7043373/ios-to-android-how-to-display-a-textview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!