Android and Layouts

后端 未结 2 995
南方客
南方客 2021-01-03 09:25

I need to locate text on view:

text \'Some more text\' should be located in bottom|center_horizontal

text \'Short text\' should be located on with align righ

相关标签:
2条回答
  • 2021-01-03 09:53

    If I understand correctly you want to simply put a serious of strings on the text view. You can do this in XML. A handy GUI tool for this would be DroidDraw You can run it in a browser or on your Desktop. I find it handy for some GUI things. Other than that you can Draw a view, something like this...

       class DrawOnTop extends View { 
        public DrawOnTop(Context context) { 
                super(context); 
                // TODO Auto-generated constructor stub 
        } 
        @Override 
        public void onDraw(Canvas canvas) { 
            Paint paint = new Paint(); 
                paint.setStyle(Paint.Style.FILL); 
                paint.setColor(Color.RED);
                paint.setTextSize(15);
                Paint paint2 = new Paint();
                canvas.drawText("Test", 30, 30, paint);
        } 
    } 
    

    In the above example I've defined a new paint. This is to set the properties of the text. I've given it a red colour and text size 15. You can add more attributes too. I've also drawn a string "Test". It is at coordinates (30,30) these are the x and y coordinates where I want Java to draw it. It's then using paint's attributes.

    EDIT: this page may also be of some help http://developer.android.com/reference/android/graphics/Canvas.html

    0 讨论(0)
  • 2021-01-03 10:00

    Here a couple quick guidelines:

    1. Android Layouts tend to be much more deeply nested than you would normally expect. You often end up with "empty" layouts that just take up space so that other elements lay out correctly.
    2. RelativeLayout is your friend whenever you are aligning text to a particular edge.
    3. Use the padding settings to put text "a little away from" an edge.
    4. Gravity aligns the text within that TextView or button.

    Looking again I your diagram, I reproduced it this way:

    1. Start with a relative layout ('fill_content') that takes up the entire screen.
    2. Put in the "short text" and "some more text" by anchoring to the top and bottom.
    3. Put a zero-width item with the property "centerInParent" for a point in the middle of the screen.
    4. Put the remaining to items above and aligned with that centerpoint.

    Unfortunately, nothing in step 4 worked correctly. Nothing like "layout_below" worked when the referenced item was a centerInParent item. with relative layouts to step 3. Turns out it had to do with failing to fill_content on the top level. Yes, the layouts are tricky, and I wish there was a debugger for them.

    Here's the correct version:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/r1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView
        android:id="@+id/short_text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Short Text"
        android:gravity="right"
        android:layout_marginTop="30dip"
        android:layout_alignParentTop="true" />
    <TextView
        android:id="@+id/more_text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Some More Text"
        android:gravity="center"
        android:layout_alignParentBottom="true" />
      <TextView android:id="@+id/centerpoint"
        android:layout_centerInParent="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:width="0dip"
        android:height="0dip"
        />
      <TextView android:id="@+id/run_fox"
        android:text="Run, fox, run!"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/centerpoint"
        android:layout_toLeftOf="@id/centerpoint" 
        />
    <TextView
        android:layout_below="@id/centerpoint"
        android:text="The quick brown fox jumped over the lazy dog, who had been a frog, and then got features and ran slowly."
        android:layout_alignRight="@id/centerpoint"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />
     </RelativeLayout>
    
    0 讨论(0)
提交回复
热议问题