自分のアクティビティの目に見えるサイズをどのように知ることができますか?
とからの高さと幅ではなくgetHeight()
、アクティビティの実際のサイズを取得しようとしています。これによりgetWidth()
、
画面がフルサイズになります。
自分のアクティビティの目に見えるサイズをどのように知ることができますか?
とからの高さと幅ではなくgetHeight()
、アクティビティの実際のサイズを取得しようとしています。これによりgetWidth()
、
画面がフルサイズになります。
とを見て、その幅/高さを取得したいと思います。Activity.getWindow()
Window.getDecorView()
私は時間を節約する答えがもっと好きです:
myActivity.getWindow().getDecorView().getHeight();
myActivity.getWindow().getDecorView().getWidth();
アクティビティの正確な高さを知る方法を見つけました。タイトルバーとタスクバーの背後にある使用可能なすべての画面にビューが表示されている場合は、を使用 View.getBottom()
してそのアクティビティの実際の高さを取得します。
ルートレイアウトを使用してこれを行いました。問題は、onCreate / onStart / onResumeメソッドを初めて実行するときに、この情報(幅と高さ)を取得できないことです。
その後いつでも、ルートレイアウト(LinearLayout / FrameLayout / TableLayout / etc)からサイズを取得できます。私の場合はFrameLayoutを使用していました。
FrameLayout f = (FrameLayout) findViewById(R.id.layout_root);
Log.d(TAG, "width " + f.getMeasuredWidth() );
Log.d(TAG, "height " + f.getMeasuredHeight() );
また:
FrameLayout f = (FrameLayout) findViewById(R.id.layout_root);
Log.d(TAG, "width " + f.getRight() );
Log.d(TAG, "height " + f.getBottom() );
ここから編集----------------------------------------------- ---------
onCretedメソッドでこの情報を取得する方法を見つけましたが、アクティビティが複数ある場合に限ります。私の場合、2番目のアクティビティを呼び出すメインアクティビティがあります。
新しいItentによってsecond_activityを呼び出す前のmain_activityで、second_activityの情報を追加できます。最初のアクティビティで必要なコードは次のとおりです。
Intent i = new Intent(this, SecondActivity.class);
i.putExtra("width", f.getMeasuredWidth());
i.putExtra("height", f.getMeasuredHeight());
startActivity(i);
その後、2番目のアクティビティで、次のことを行う2番目のアクティビティのonCreateメソッドでこの情報を取得できます。
int width = getIntent().getExtras().getInt("width");
int height = getIntent().getExtras().getInt("height");
関数を使用して正しいビューサイズを取得する簡単な方法がありgetDefaultSize
ます。アクティビティ全体にまたがるビューの場合、次のアプローチを使用してアクティビティクライアントリージョンのディメンションを取得できます。
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
int viewWidth = getDefaultSize(getWidth(), widthMeasureSpec);
int viewHeight = getDefaultSize(getHeight(), heightMeasureSpec);
...
}
ビューをアクティビティ全体に拡張するには:
MyView testView = new MyView(this);
LinearLayout testViewGroup = new LinearLayout(this);
testView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT);
testViewGroup.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT);
testViewGroup.addView(testView);
setContentView(testViewGroup);
DataBinding(使用する必要があります)を使用している場合は、作成したbinding.getRoot().getHeight()
場所または他のそのようなメソッドを呼び出すことができます。レイアウトのサイズが大きくなるまで待つ必要があります。これは次のように実行できます。binding
ViewDataBinding
DataBindingUtil.setContentView()
DataBindingUtil
binding.getRoot().getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
binding.getRoot().getViewTreeObserver().removeOnGlobalLayoutListener(this);
int height = binding.getRoot().getHeight();
}
});
OnGlobalLayoutListenerを削除する必要があることに注意してください。そうしないと、レイアウトで何かが変更されるたびにOnGlobalLayoutListenerが実行されます。
アクティビティの高さを取得します。
public static int getActivityHeight(Activity activity)
{
return activity.findViewById(android.R.id.content).getHeight();
}
アクティビティの幅を取得する
public static int getActivityWidth(Activity activity)
{
return activity.findViewById(android.R.id.content).getWidth();
}
onCreate()/ onStart()/ onResume()メソッドで高さと幅を0として取得します。理由はサイズがまだ設定されていないためです。現在のサイズを知りたい場合は、この方法の1つでオブザーバーを使用し、現在のサイズを取得するときに必要なことを実行することをお勧めします。
Constraint_layoutは、次のコードのレイアウトルート要素です。
ConstraintLayout constraintLayout = (ConstraintLayout) findViewById(R.id.constraint_layout);
constraintLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
constraintLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
int width = constraintLayout.getWidth();
int height = constraintLayout.getHeight();
doSmthMethod(width, height);
}
});