Android:按钮存在(onCreate 已完成)但仍然没有 width/height

Android: Button exists (onCreate has finished) but still has no width/height

想要的场景:我的应用程序已经启动并且可见。我按下按钮 A 并想显示按钮 bMap 的宽度。总之我不工作的方法:

// Is called by button bQuestions' onClick
public void QuestionsButton(View v) {

    Context context = getApplicationContext();

    // getting button bMap
    Button bMap = (Button)findViewById(R.id.bMap);


    // getting the width of button bMap
    int width = bMap.getMeasuredWidth(); // getWidht() also returns 0
    CharSequence text = String.valueOf(width);

    int duration = Toast.LENGTH_SHORT;

    Toast.makeText(context, text, duration).show();

}

我知道这个问题至少被问过一百万次,但在所有这些情况下,getWidth() 被调用得太早了。就我而言,这不是因为我必须按另一个现有按钮来调用此方法。为什么 bMap 在它已经存在很久之后仍然具有 0 的宽度?

我确实发现,如果我先按 bMap en 将视图另存为变量,它确实有效。但是,在另一个按钮的功能起作用之前先让一个按钮被按下当然不是一个选项。

编辑 完成实施: MainscreenActivity.java:

package com.jswebcom.jeroensak.zuydopendag;

import android.content.Context;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
import android.widget.Button;
import android.widget.Toast;


public class MainscreenActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_mainscreen);
    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment())
                .commit();
    }

}

public void ActivityButton(View v) {

    Context context = getApplicationContext();
    CharSequence text = "Activiteiten";
    int duration = Toast.LENGTH_SHORT;

    Toast.makeText(context, text, duration).show();

}

public void MapButton(View v) {

    Context context = getApplicationContext();
    CharSequence text = "Plattegrond";
    int duration = Toast.LENGTH_SHORT;

    Toast.makeText(context, text, duration).show();


}

public void InfoButton(View v) {

    Context context = getApplicationContext();
    CharSequence text = "Info";
    int duration = Toast.LENGTH_SHORT;

    Toast.makeText(context, text, duration).show();

}

public void QuizButton(View v) {

    Context context = getApplicationContext();
    CharSequence text = "Quizvraag";
    int duration = Toast.LENGTH_SHORT;

    Toast.makeText(context, text, duration).show();

}

public void QuestionsButton(View v) {

    Context context = getApplicationContext();

    Button bMap = (Button)findViewById(R.id.bMap);

    int width = bMap.getMeasuredWidth();
    CharSequence text = String.valueOf(width);

    int duration = Toast.LENGTH_SHORT;

    Toast.makeText(context, text, duration).show();


    bMap.animate().translationX(v.getWidth());

}




@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_mainscreen, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.activity_mainscreen, container, false);
        return rootView;
    }
}
}

activity_mainscreen.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainscreenActivity$PlaceholderFragment"
android:weightSum="2"
android:padding="0dp"
android:id="@+id/container">

<LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1">

    <Button
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_weight="1"
        android:backgroundTint="#ff005baa"
        android:background="#ff005baa"
        android:text="@string/activiteiten"
        android:textColor="#ffffff"
        android:textSize="25sp"
        android:id="@+id/bActivity"
        android:onClick="ActivityButton" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_weight="1"
        android:background="#ff66beff"
        android:backgroundTint="#ff66beff"
        android:text="@string/info"
        android:textColor="#ffffff"
        android:textSize="25sp"
        android:onClick="InfoButton"
        android:id="@+id/bInfo" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_weight="1"
        android:backgroundTint="#ff0077c0"
        android:background="#ff0077c0"
        android:text="@string/vragen"
        android:textColor="#ffffff"
        android:textSize="25sp"
        android:onClick="QuestionsButton"
        android:id="@+id/bQuestions" />


</LinearLayout>

<LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1">

    <Button
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_weight="1"
        android:background="#ff66beff"
        android:backgroundTint="#ff66beff"
        android:text="@string/plattegrond"
        android:textColor="#ffffff"
        android:textSize="25sp"
        android:onClick="MapButton"
        android:id="@+id/bMap" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_weight="1"
        android:background="#ff0077c0"
        android:backgroundTint="#ff0077c0"
        android:text="@string/quizvraag"
        android:textColor="#ffffff"
        android:textSize="25sp"
        android:onClick="QuizButton"
        android:id="@+id/bQuiz" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_weight="1"
        android:background="#ffffffff"
        android:id="@+id/bLogo" />


</LinearLayout>

调用前

bMap.getMeasuredWidth();

您必须致电:

bMap.measure(0,0);

来自API:

视图的实际测量工作是在onMeasure(int, int)中进行的,该方法调用

所以调用measure()开始实际测量,只有在此之后,getMeasuredWidth()和getMeasuredHeight()才会起作用。