【发布时间】:2016-12-16 18:37:05
【问题描述】:
我正在尝试生成一个bitmap 以显示在通知中。布局有点复杂,但是我需要根据屏幕大小设置宽度。在 XML 中,我在根目录中有一个 LinearLayout,并使用
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 中有很多关于 i 的问题,但是,所有这些问题都涉及对已经有 rootView 的布局进行膨胀,但对我没有任何帮助。有几个答案建议我在视图上调用 measure() 方法。但是,在调用 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);
此可用宽度是动态计算的。但是,当我后来尝试使用 v.layout() 从这个膨胀的视图生成位图时,它一直在生成一个不符合我设置的宽度的图像。
后来,我遇到了this SO 线程,我注意到在膨胀时,指定了new LinearLayoutParams(contex),而不是指定null 作为父视图。我试过了,现在,动态设置的宽度得到了尊重。
【问题讨论】:
-
这是状态栏通知吗?
-
用于设置通知中的 bigContentView。谢谢
-
那我不明白你的问题。状态栏通知 UI 是使用
RemoteViews配置的,而不是通过正常的视图膨胀和修改。也许您应该澄清您的要求并包含其他代码 sn-ps。 -
是的,我正在使用 RemoteViews。在那个远程视图中,我有一个 imageView,它是通过膨胀另一个布局并在其中填充内容来生成位图的。后来我将该位图设置到该图像视图中。我刚刚看到另一个问题stackoverflow.com/questions/18565226/… 并注意到在膨胀时,我们将 new LinearLayout(context) 作为 rootView。我尝试了同样的方法,但它不起作用!非常感谢您的帮助:)
-
请包含更多相关代码sn-ps。没有办法知道您在做什么(或是否)与您刚刚发布的链接不同。至少,edit 您的帖子会有所帮助,如果这正是您正在使用的代码,请包含该链接。
标签: android android-linearlayout android-notifications android-inflate