Android devGuide 解释了如何使用问号(?)代替at(@)来引用当前应用主题中属性的值.
有没有人知道如何从代码中做到这一点,例如在自定义组件中?
我正在使用Jersey Client来访问web服务,如下所示:
response =
r.accept(MediaType.TEXT_PLAIN_TYPE).header("content-length", 0).post(String.class);
其中r是WebResource
但是,Webservice返回411 - 缺少Content-Length.
使用tcpdump,我发现我能够指定自定义标题,即.header("myheader", 0)工作正常.
因此,由于某些原因,球衣似乎正在删除内容长度标题.
有人有什么想法吗?
我用XML制作了一个自定义组件,它包含一个按钮,其上面叠有一个imageview:
<myapp.widget.ClearableCaptionedButton
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="@+id/ccbutton_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical|left"
android:textAppearance="?android:attr/textAppearanceMedium"
android:background="@android:drawable/edit_text"/>
<ImageView
android:id="@+id/ccbutton_clear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dip"
android:layout_alignRight="@id/ccbutton_button"
android:layout_alignTop="@id/ccbutton_button"
android:layout_alignBottom="@id/ccbutton_button"/>
</myapp.widget.ClearableCaptionedButton>
Run Code Online (Sandbox Code Playgroud)
java源代码摘录:
public class ClearableCaptionedButton extends RelativeLayout implements OnClickListener {
...
public ClearableCaptionedButton(Context context, AttributeSet attrs) {
super(context, attrs);
// some stuff that works fine
}
..
protected void onFinishInflate() {
super.onFinishInflate();
mButton = (Button) findViewById(R.id.ccbutton_button);
mClear = (ImageView) findViewById(R.id.ccbutton_clear);
mButton.setText(""); // error here: mButton == null
}
Run Code Online (Sandbox Code Playgroud)
我的问题与此类似.当我尝试在自定义化合物中查找视图时,findViewById返回null.但是,正如你所看到的,我已经添加了super(context,attrs); 到构造函数.我在xml布局中直接使用自定义组件,如下所示:
<LinearLayout>
<!-- some stuff -->
<myapp.widget.ClearableCaptionedButton
android:layout_width="fill_parent"
android:layout_height="wrap_content" …Run Code Online (Sandbox Code Playgroud)