【发布时间】:2016-05-17 13:41:44
【问题描述】:
我试图在 TextView 下画一条线(创建一个橡胶桥记分表,其中有问题的线反映了虚线 here)。我不知道如何获取最底部 TextView 的位置并使用这些数字在正确的位置创建一条线。该行目前远低于 TextView,它应该在它的正下方,即:
40
———
40
——
线条确实会根据最底部 TextView 的垂直位置改变位置,所以我认为我成功地检索了它的位置,但不知何故我没有正确使用它来绘制线条。
下面是获取TextView的位置并将其发送到画线方法的代码:
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);
}
}
);
这是画线的方法:
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; i < gameLines.size(); ++i ){
ShapeDrawable gameLine = gameLines.get(i);
int position = gameLinePositions.get(i);
gameLine.setBounds(0, position-2, width, position+2);
gameLine.draw(canvas);
}
}
如果有更多有用的信息,请告诉我!这似乎是一个微妙的问题,有点难以描述,但我认为我对 android 的布局或绘图管理的无知主要是错误的。
编辑这里的主要布局。它很长,但我认为几乎所有相关的东西都在顶部
<?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>
【问题讨论】:
-
你有布局xml吗?
-
我会发布它,虽然它有点长。不确定其中有多少是相关的
-
基本上,我使用 FrameLayout 将主要布局(所有 LinearLayouts 和其他东西)放在我用于绘制线条的 BackgroundLines 自定义布局之上。我认为这将是一种简单的设置方法,因为尝试拼凑一个布局,包括一堆包含线条的奇怪形状的视图会非常复杂
-
你尝试过相对布局吗?
-
在项目开始时简要介绍,但当时这种方法似乎更容易。您认为将其重组为与文本在同一层中的行的相对布局会更好吗?
标签: android android-layout android-shapedrawable