Android 膨胀视图不遵守动态设置的宽度
Android inflate view not honoring dynamically set width
我正在尝试生成 bitmap
以在通知中显示。布局有点复杂,但是,我需要根据屏幕尺寸设置宽度。在 XML 中,我在根目录中有一个 LinearLayout
,我用
膨胀视图
LinearLayout v = (LinearLayout) i inflater.inflate(R.layout.notification_expanded_detail, null, false);
此版式有很多我需要填充的元素。并且它们的宽度取决于布局本身的宽度并且布局的宽度随phone而变化。我用
设置膨胀布局的宽度
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams((int) available_width, LinearLayout.LayoutParams.WRAP_CONTENT);
v.setLayoutParams(params);
但是,当我尝试从中生成 bitmap
时,使用 setLayoutParams
设置的大小似乎不会影响膨胀布局的子视图。 SO 中围绕 i 有很多问题,但是,所有这些问题都涉及膨胀已经具有 rootView
的布局,但对我没有任何作用。有几个答案建议我在视图上调用 measure()
方法。但是,在调用测量方法后,getMeasuredWidth
returns 宽度小于我在 setLayoutParams
调用中设置的宽度。
更新
关于这个问题的更多背景信息。我试图显示一个通知,它有一个占位符 ImageView,稍后我会用从 XML 膨胀的视图生成的位图填充它。远程视图布局为
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/sliderViewBottom"
android:layout_width="match_parent"
android:layout_height="256dp"
android:background="@color/detail_page_blue"
android:clickable="true"
android:focusable="false"
android:orientation="vertical"
android:gravity="center"
android:padding="10dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@color/white"
>
<ImageView
android:id="@+id/progressImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#D05757"
android:gravity="center"
android:paddingBottom="8dp"
android:paddingTop="8dp"
>
<TextView
android:id="@+id/overAllDelay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Delayed by "
android:textColor="@color/white"
/>
<TextView
android:id="@+id/overAllDelayVal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/overAllDelay"
android:text="-"
android:textColor="@color/white"
/>
</RelativeLayout>
</LinearLayout>
我试图膨胀并生成位图以在 progressImage
中设置的布局是
<RelativeLayout
android:id="@+id/swipe_page_track_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="10dp"
android:paddingLeft="@dimen/swipe_layout_info_card_track_margin"
android:paddingRight="@dimen/swipe_layout_info_card_track_margin"
>
<ImageView
android:id="@+id/fromGreenCircle"
android:layout_width="10dp"
android:layout_height="10dp"
android:layout_alignParentLeft="true"
android:layout_marginTop="1dp"
android:src="@drawable/from_green_circle"/>
<ImageView
android:layout_width="10dp"
android:layout_height="10dp"
android:layout_alignParentRight="true"
android:layout_marginTop="1dp"
android:src="@drawable/station_end"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="3dp"
android:layout_alignParentRight="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="4.5dp"
android:background="@drawable/swipe_page_empty_track"
/>
<ImageView
android:id="@+id/fromGreenTrack"
android:layout_width="105dp"
android:layout_height="3dp"
android:layout_alignParentLeft="true"
android:layout_marginTop="4.5dp"
android:src="@drawable/from_green_track"
/>
<ImageView
android:id="@+id/trackNavigationIcon"
android:layout_width="12dp"
android:layout_height="12dp"
android:layout_marginLeft="100dp"
android:src="@drawable/ic_track_detail_card_current_station_pointer"
/>
<TextView
android:id="@+id/currentStatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/trackNavigationIcon"
android:layout_marginTop="10dp"
android:textColor="@color/black"
/>
</RelativeLayout>
我试图用
膨胀第二个布局
LinearLayout v = (LinearLayout) i inflater.inflate(R.layout.notification_expanded_detail, null, false);
然后在用
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams((int) available_width, LinearLayout.LayoutParams.WRAP_CONTENT);
v.setLayoutParams(params);
这个available_width是动态计算的。但是,当我后来尝试使用 v.layout()
从这个膨胀视图生成位图时,它一直在生成不符合我设置的宽度的图像。
后来,我遇到了 this SO 线程,我注意到在膨胀时,没有将 null 指定为 parentView,而是指定了 new LinearLayoutParams(contex)
。我试过了,现在,动态设置的宽度得到了尊重。
我遇到了这个 SO 线程,我注意到在膨胀时,没有将 null 指定为 parentView,而是指定了 new LinearLayoutParams(contex)。我试过了,现在,动态设置的宽度得到了尊重
我正在尝试生成 bitmap
以在通知中显示。布局有点复杂,但是,我需要根据屏幕尺寸设置宽度。在 XML 中,我在根目录中有一个 LinearLayout
,我用
LinearLayout v = (LinearLayout) i inflater.inflate(R.layout.notification_expanded_detail, null, false);
此版式有很多我需要填充的元素。并且它们的宽度取决于布局本身的宽度并且布局的宽度随phone而变化。我用
设置膨胀布局的宽度LinearLayout.LayoutParams params = new LinearLayout.LayoutParams((int) available_width, LinearLayout.LayoutParams.WRAP_CONTENT);
v.setLayoutParams(params);
但是,当我尝试从中生成 bitmap
时,使用 setLayoutParams
设置的大小似乎不会影响膨胀布局的子视图。 SO 中围绕 i 有很多问题,但是,所有这些问题都涉及膨胀已经具有 rootView
的布局,但对我没有任何作用。有几个答案建议我在视图上调用 measure()
方法。但是,在调用测量方法后,getMeasuredWidth
returns 宽度小于我在 setLayoutParams
调用中设置的宽度。
更新
关于这个问题的更多背景信息。我试图显示一个通知,它有一个占位符 ImageView,稍后我会用从 XML 膨胀的视图生成的位图填充它。远程视图布局为
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/sliderViewBottom"
android:layout_width="match_parent"
android:layout_height="256dp"
android:background="@color/detail_page_blue"
android:clickable="true"
android:focusable="false"
android:orientation="vertical"
android:gravity="center"
android:padding="10dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@color/white"
>
<ImageView
android:id="@+id/progressImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#D05757"
android:gravity="center"
android:paddingBottom="8dp"
android:paddingTop="8dp"
>
<TextView
android:id="@+id/overAllDelay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Delayed by "
android:textColor="@color/white"
/>
<TextView
android:id="@+id/overAllDelayVal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/overAllDelay"
android:text="-"
android:textColor="@color/white"
/>
</RelativeLayout>
</LinearLayout>
我试图膨胀并生成位图以在 progressImage
中设置的布局是
<RelativeLayout
android:id="@+id/swipe_page_track_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="10dp"
android:paddingLeft="@dimen/swipe_layout_info_card_track_margin"
android:paddingRight="@dimen/swipe_layout_info_card_track_margin"
>
<ImageView
android:id="@+id/fromGreenCircle"
android:layout_width="10dp"
android:layout_height="10dp"
android:layout_alignParentLeft="true"
android:layout_marginTop="1dp"
android:src="@drawable/from_green_circle"/>
<ImageView
android:layout_width="10dp"
android:layout_height="10dp"
android:layout_alignParentRight="true"
android:layout_marginTop="1dp"
android:src="@drawable/station_end"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="3dp"
android:layout_alignParentRight="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="4.5dp"
android:background="@drawable/swipe_page_empty_track"
/>
<ImageView
android:id="@+id/fromGreenTrack"
android:layout_width="105dp"
android:layout_height="3dp"
android:layout_alignParentLeft="true"
android:layout_marginTop="4.5dp"
android:src="@drawable/from_green_track"
/>
<ImageView
android:id="@+id/trackNavigationIcon"
android:layout_width="12dp"
android:layout_height="12dp"
android:layout_marginLeft="100dp"
android:src="@drawable/ic_track_detail_card_current_station_pointer"
/>
<TextView
android:id="@+id/currentStatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/trackNavigationIcon"
android:layout_marginTop="10dp"
android:textColor="@color/black"
/>
</RelativeLayout>
我试图用
膨胀第二个布局 LinearLayout v = (LinearLayout) i inflater.inflate(R.layout.notification_expanded_detail, null, false);
然后在用
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams((int) available_width, LinearLayout.LayoutParams.WRAP_CONTENT);
v.setLayoutParams(params);
这个available_width是动态计算的。但是,当我后来尝试使用 v.layout()
从这个膨胀视图生成位图时,它一直在生成不符合我设置的宽度的图像。
后来,我遇到了 this SO 线程,我注意到在膨胀时,没有将 null 指定为 parentView,而是指定了 new LinearLayoutParams(contex)
。我试过了,现在,动态设置的宽度得到了尊重。
我遇到了这个 SO 线程,我注意到在膨胀时,没有将 null 指定为 parentView,而是指定了 new LinearLayoutParams(contex)。我试过了,现在,动态设置的宽度得到了尊重