编辑: 我刚刚发现XML导致了这个错误。当我删除" selectiontable" (drawroom使用android:layout_toRightOf =" @ + id / selectiontable")我为drawroom得到了正确的值。我设法通过在LinearLayout而不是RelativeLayout中放置selecionable和drawroom来修复它,并对XML进行了一些更改。
我有一个奇怪的问题。在另一个类中,我的代码几乎相同,并且在那里工作得很好,但是在这个类中,getHeight和getWidth的值总是错误的,并且看起来像是纵向布局的值,即使手机处于横向模式并且我在清单文件中声明了它(android:screenOrientation =" sensorLandscape")。我在创建布局后捕获它们时得到正确的值(在按下按钮后测试它以捕获它们)。这真的很奇怪,因为它的工作方式几乎与我的其他课程完全一样。 这是代码:
public class SortMaterialActivity extends AppCompatActivity
{
private LinearLayout drawroom;
...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sort_material);
drawroom = (LinearLayout)findViewById(R.id.drawroom);
...
drawroom.post(new Runnable() {
@Override
public void run() {
createCanvas();
calcDensity();
createMaterial();
}
});
}
private void createCanvas()
{
canvasWidth = drawroom.getWidth(); //wrong values here
canvasHeight = drawroom.getHeight();
storageroomHeight = storageroom.getHeight();
Bitmap bitmap = Bitmap.createBitmap(canvasWidth, canvasHeight, Bitmap.Config.ARGB_8888);
canvas = new Canvas(bitmap);
canvasFrame.setImageBitmap(bitmap);
}
这是XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/selectiontable"
android:orientation="vertical"
android:layout_alignParentLeft="true"
android:layout_marginLeft="5dp"
android:layout_marginRight="-5dp"
android:layout_width="wrap_content"
android:layout_height="match_parent">
<android.support.percent.PercentRelativeLayout
android:layout_width="match_parent"
android:layout_weight="0.125"
android:layout_height="0dp"
android:layout_marginTop="5dp">
<ImageButton
android:id="@+id/exitButton"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_heightPercent="100%"
app:layout_aspectRatio="100%"
android:onClick="onExitButton"
android:background="@drawable/exit_button" />
</android.support.percent.PercentRelativeLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.75">
</FrameLayout>
<android.support.percent.PercentRelativeLayout
android:layout_width="wrap_content"
android:layout_weight="0.125"
android:layout_height="0dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp">
<ImageButton
android:id="@+id/checkResultButton"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_heightPercent="100%"
app:layout_aspectRatio="100%"
android:onClick="onCheckResultButton"
android:background="@drawable/check_button"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
</android.support.percent.PercentRelativeLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/drawroom"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:layout_toRightOf="@+id/selectiontable"
android:orientation="vertical">
<FrameLayout
android:id="@+id/storageroom"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.4"
android:background="@drawable/storage_boundaries">
</FrameLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.6"
android:orientation="horizontal">
<FrameLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:background="#FF8A80">
</FrameLayout>
<FrameLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:background="#8C9EFF">
</FrameLayout>
<FrameLayout
android:id="@+id/cubeframe"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:background="#A5D6A7">
</FrameLayout>
</LinearLayout>
</LinearLayout>
<ImageView
android:id="@+id/canvas"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignLeft="@+id/drawroom"
android:layout_alignRight="@+id/drawroom"
android:layout_alignBottom="@+id/drawroom"
android:layout_alignTop="@id/drawroom">
</ImageView>
<RelativeLayout
android:id="@+id/overlay"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/black_overlay"
android:onClick="onBackButton"
android:visibility="gone">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@drawable/back">
<LinearLayout
android:id="@+id/resultButtons"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:orientation="horizontal"
android:clickable="true"
android:onClick="onShowOptimizationButton">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:orientation="vertical">
<ImageView
android:id="@+id/showOptimizationView"
android:layout_width="150dp"
android:layout_height="150dp"/>
<TextView
android:id="@+id/showOptimizationText"
android:layout_width="150dp"
android:maxLines="1"
android:text="falsch"
android:textSize="20dp"
android:textStyle="bold"
android:layout_height="wrap_content"
android:textAlignment="center"
android:gravity="center"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:orientation="vertical">
<ImageView
android:id="@+id/showResultInfoView"
android:visibility="gone"
android:layout_width="100dp"
android:layout_height="100dp"/>
<TextView
android:id="@+id/showResultInfoText"
android:layout_width="wrap_content"
android:layout_gravity="center_horizontal"
android:maxLines="1"
android:textSize="20dp"
android:textStyle="bold"
android:layout_height="wrap_content"
android:textAlignment="center"
android:gravity="center"/>
</LinearLayout>
</LinearLayout>
<ImageButton
android:id="@+id/backButton"
android:background="@drawable/ic_arrow_back_black_48dp"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:onClick="onBackButton"
android:layout_below="@id/resultButtons"/>
<TextView
android:id="@+id/backButtonText"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text="zurück"
android:layout_marginBottom="5dp"
android:textAlignment="center"
android:gravity="center"
android:layout_below="@+id/backButton"
android:layout_alignLeft="@+id/backButton"/>
<ImageButton
android:id="@+id/newtaskButton"
android:background="@drawable/ic_replay_black_48dp"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:onClick="onRepeatButton"
android:layout_toRightOf="@+id/backButton"
android:layout_below="@id/resultButtons"/>
<TextView
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="neue Aufgabe"
android:textAlignment="center"
android:gravity="center"
android:layout_below="@+id/newtaskButton"
android:layout_alignLeft="@+id/newtaskButton"/>
<ImageButton
android:id="@+id/OverlayExitButton"
android:background="@drawable/exit_button"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:onClick="onExitButton"
android:layout_toRightOf="@+id/newtaskButton"
android:layout_below="@id/resultButtons"/>
<TextView
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="beenden"
android:textAlignment="center"
android:gravity="center"
android:layout_below="@+id/OverlayExitButton"
android:layout_alignLeft="@+id/OverlayExitButton"/>
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>