android - How to draw relative to the position of another view -


i'm trying draw line under textview (creating rubber bridge score sheet, lines in question mirror dotted lines here). can't figure out how position of bottommost textview , use numbers create line in proper position. line far below textview should below, i.e.:

40
——— <---- want (close number)

40

——— <---- (farther number)

the line change position depending on vertical position of bottommost textview, think retrieving position, somehow i'm not using draw line.

here's code getting position of textview , sending line drawing method:

    findviewbyid(r.id.main).getviewtreeobserver().addongloballayoutlistener(             new viewtreeobserver.ongloballayoutlistener() {                 @override                 public void ongloballayout() {                     //remove listener                     if( build.version.sdk_int >= build.version_codes.jelly_bean )                         findviewbyid(r.id.main).getviewtreeobserver()                                 .removeongloballayoutlistener(this);                     else                         findviewbyid(r.id.main).getviewtreeobserver()                                 .removeglobalonlayoutlistener(this);                      textview bottomtextview = (textview) wegamelayout                             .getchildat(wegamelayout.getchildcount() - 1);                     int[] pos = new int [2];                     bottomtextview.getlocationonscreen(pos);                     int gamelineposition = pos[1];                     int height = bottomtextview.getheight();                     ((backgroundlines)findviewbyid(r.id.background_lines))                             .addgameline(gamelineposition + height - topdiff);                 }             }     ); 

and here's line drawing method:

public void addgameline(int position) {     shapedrawable line = new shapedrawable(new rectshape());     gamelines.add(line);     gamelinepositions.add(position);     invalidate(); } @override public void ondraw(canvas canvas) {     int width = this.getwidth();     for( int i=0; < gamelines.size(); ++i ){         shapedrawable gameline = gamelines.get(i);         int position = gamelinepositions.get(i);         gameline.setbounds(0, position-2, width, position+2);         gameline.draw(canvas);     } } 

let me know if more info useful! seems subtle problem , little tough describe, think ignorance of android's layout or drawing management @ fault.

edit here's main layout. it's long, relevant stuff right @ top think

<?xml version="1.0" encoding="utf-8"?>  <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:orientation="vertical">      <android.support.v7.widget.toolbar         android:id="@+id/toolbar"         android:layout_width="match_parent"         android:layout_height="?attr/actionbarsize"         android:elevation="4dp"         android:theme="@style/themeoverlay.appcompat.actionbar" />      <framelayout xmlns:android="http://schemas.android.com/apk/res/android"         xmlns:tools="http://schemas.android.com/tools"         android:id="@+id/main"         android:layout_width="match_parent"         android:layout_height="match_parent"         android:paddingbottom="@dimen/activity_vertical_margin"         android:paddingleft="@dimen/activity_horizontal_margin"         android:paddingright="@dimen/activity_horizontal_margin"         android:paddingtop="@dimen/activity_vertical_margin"         android:background="@drawable/parchment_scroll_background"         tools:context="com.pianomansb.betterbridgescoresheet.mainactivity">          <com.pianomansb.betterbridgescoresheet.backgroundlines             android:layout_width="match_parent"             android:layout_height="match_parent"             android:id="@+id/background_lines" />          <linearlayout             android:layout_width="match_parent"             android:layout_height="match_parent"             android:orientation="horizontal">              <linearlayout                 android:id="@+id/we_column"                 android:layout_width="0dp"                 android:layout_weight="1"                 android:layout_height="match_parent"                 android:orientation="vertical">                  <linearlayout                     android:id="@+id/we_upper"                     android:layout_width="match_parent"                     android:layout_height="0dp"                     android:layout_weight="1"                     android:orientation="vertical">                      <textview                         android:id="@+id/we_text"                         android:layout_width="wrap_content"                         android:layout_height="wrap_content"                         android:layout_gravity="center_horizontal"                         android:text="@string/we_text"                         style="@style/wetheyfont"/>                      <linearlayout                         android:id="@+id/we_bonus"                         android:layout_width="match_parent"                         android:layout_height="match_parent"                         android:orientation="vertical"                         android:gravity="bottom">                      </linearlayout>                  </linearlayout>                  <linearlayout                     android:id="@+id/we_game"                     android:layout_width="match_parent"                     android:layout_height="0dp"                     android:layout_weight="1"                     android:orientation="vertical">                  </linearlayout>              </linearlayout>              <linearlayout                 android:id="@+id/they_column"                 android:layout_width="0dp"                 android:layout_weight="1"                 android:layout_height="match_parent"                 android:orientation="vertical">                  <linearlayout                     android:id="@+id/they_upper"                     android:layout_width="match_parent"                     android:layout_height="0dp"                     android:layout_weight="1"                     android:orientation="vertical">                      <textview                         android:id="@+id/they_text"                         android:layout_width="wrap_content"                         android:layout_height="wrap_content"                         android:layout_gravity="center_horizontal"                         android:text="@string/they_text"                         style="@style/wetheyfont"/>                      <linearlayout                         android:id="@+id/they_bonus"                         android:layout_width="match_parent"                         android:layout_height="match_parent"                         android:orientation="vertical"                         android:gravity="bottom">                      </linearlayout>                  </linearlayout>                  <linearlayout                     android:id="@+id/they_game"                     android:layout_width="match_parent"                     android:layout_height="0dp"                     android:layout_weight="1"                     android:orientation="vertical">                  </linearlayout>              </linearlayout>         </linearlayout>     </framelayout> </linearlayout> 

it sounds you're trying more complicated usual view layout tools let do. won't tell stop trying way, recommend different approach might easier you.

i make entire thing custom view (unless can tall, might whole different issue. make sure view subclass advertises preferred height , width properly.

in ondraw draw lines on canvas would. note can draw text on canvas without need textview. use canvas.drawtext() position text way want, , use method's paint parameter size , color text.

i hope proves better strategy you.


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -