问题
I am using a video view and a relative layout. The app will only run in portrait mode. Currently, I am giving exact dps to get the desired size. What I would like to have is to get the video view with half the size of my layout.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:opencv="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_smile"
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"
tools:context="MyActivity">
<VideoView
android:id="@+id/videoViewSmile"
android:layout_width="match_parent"
android:layout_height="300dp" />
</RelativeLayout>
回答1:
First thing, you can get both height and width using an android.Util
class called DisplayMetrics
:
DisplayMetrics displayMetrics = new DisplayMetrics();
And then insert the current display info into the declared DisplayMetrics
:
getActivity().getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
Now you can get the height and width of your screen by using :
displayMetrics.widthPixels // Width
displayMetrics.heightPixels // Height
It is measured in pixels, and you can change the height by :
videoView.getLayoutParams().height = displayMetrics.heightPixels / 2;
回答2:
you can get the height and width by using
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
mScreenWidth = displayMetrics.widthPixels;
mScreenHeight = displayMetrics.heightPixels;
回答3:
Easiest way is to put in an invisible view and use layout_weights
but that is not the correct way in my opinion. I would use a ViewTreeObserver
final RelativeLayout layout = (RelativeLayout)findViewById(R.id.your_layout);
ViewTreeObserver vto = layout.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
VideoView videoView = layout.findViewById(R.id.videoViewSmile);
ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(layout.getWidth(), layout.getHeight() / 2)
videoView.setLayoutParams(layoutParams);
videoView.invalidate();
layout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
});
Not tested, but i hope you see the general idea
回答4:
Try using weight feature of linearlayout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:opencv="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_smile"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="MyActivity">
<VideoView
android:id="@+id/videoViewSmile"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.5" />
<RelativeLayout
android:id="@+id/remaining_fifty_percent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.5">
</RelativeLayout>
</LinearLayout>
来源:https://stackoverflow.com/questions/42686351/how-can-i-make-my-video-view-to-half-the-screen-of-my-device