android充气视图不尊重动态设置宽度
我试图生成一个bitmap
显示在通知中。布局有点复杂,但是,我需要根据屏幕尺寸设置宽度。在XML中,我在根LinearLayout
和我膨胀与android充气视图不尊重动态设置宽度
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);
但是设置宽度为膨胀的布局,当我尝试生成这个bitmap
,似乎与setLayoutParams
设置的大小是不影响充气布局的子视图。 SO中有很多关于我的问题,但是,它们都是在扩充已经有rootView
的布局,而且没有任何效果。一些答案建议我在视图上调用measure()
方法。但是,在调用度量方法之后,getMeasuredWidth
返回的宽度稍小于我用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作为父视图,指定new LinearLayoutParams(contex)
。我试过,现在,动态设置的宽度正在兑现
我碰到这个SO线程,我注意到,虽然膨胀,而不是指定null作为父视图,指定了新的LinearLayoutParams(contex)。我试过,现在,动态设置的宽度正在兑现
这是为了状态栏通知? – Karakuri
它用于在通知中设置bigContentView。谢谢 –
然后我不明白你的问题。状态栏通知UI是使用'RemoteViews'配置的,而不是通过正常的视图膨胀和修改。也许你应该澄清你在问什么,并包含额外的代码片段。 – Karakuri