我正在使用AndroidSlidingUpPanel库。 https://github.com/umano/AndroidSlidingUpPanel
我的activity_main.xml有两个孩子。第一个孩子是主要的布局。第二个孩子是向上滑动面板的布局。 这里我发布了Sliding Up Panel的布局。 有4个LinearLayouts定义为15:15:60:10%的比例百分比。 第一个布局将在面板中显示。现在我希望面板高度是第一个布局的3/4。所以这里,第一个布局占据了屏幕的15%,但面板高度应该是屏幕的10-12%左右。
我可以使用任何方法或公式来获取屏幕大小,并根据此处提供的xml文件的第一个布局设置面板高度。
我试图在Java代码中定义面板高度。但到目前为止没有成功。 有时第二个布局也会显示在面板中,有时只有第一个布局的一半或1/4。
PS:有没有办法在setContentView()之前找到视图的高度?
<LinearLayout
android:layout_width="match_parent"
android:layout_height="5dp"
android:layout_weight="1.5"
android:background="#666666" >
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="5dp"
android:layout_weight="1.5"
android:background="#888888" >
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="6"
android:background="#eeeeee" >
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#202033" >
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="5dp"
android:layout_weight="1.5"
android:background="#666666" >
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="5dp"
android:layout_weight="1.5"
android:background="#888888" >
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="6"
android:background="#eeeeee" >
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#202033" >
</LinearLayout>
答案 0 :(得分:4)
可以以像素为单位获取屏幕的高度。也可以获得屏幕密度(DPI)。 两者的比例将为您提供屏幕的物理高度。
DisplayMetrics displayMetrics = this.getResources().getDisplayMetrics();
float dpHeight = displayMetrics.heightPixels / displayMetrics.density;
float dpWidth = displayMetrics.widthPixels / displayMetrics.density;
获取dpWidth(这是一个与密度无关的物理量)后,您可以轻松地对其应用百分比。