Java 如何将引用与自定义属性一起使用?
我已经定义了一个自定义小部件(MyWidget)Java 如何将引用与自定义属性一起使用?,java,android,attributes,Java,Android,Attributes,我已经定义了一个自定义小部件(MyWidget) res/layout/mywidget.xml res/values/attrs.xml 代码 谢谢为您的自定义视图定义附加属性 它在你链接的帖子中说明了你需要做什么。通过将xmlns:myApp=”“添加到布局中的第一个标记,将属性包括在xml中。然后,您可以将myApp:myattr添加到其中的任何标记中。重要的是attrs.xml和布局中的xmlns行感谢您的回答。就像你说的,给定的链接非常有趣。但我不知道它在哪里使用引用格式取回
- res/layout/mywidget.xml
- res/values/attrs.xml
谢谢为您的自定义视图定义附加属性
它在你链接的帖子中说明了你需要做什么。通过将xmlns:myApp=”“添加到布局中的第一个标记,将属性包括在xml中。然后,您可以将myApp:myattr添加到其中的任何标记中。重要的是attrs.xml和布局中的xmlns行感谢您的回答。就像你说的,给定的链接非常有趣。但我不知道它在哪里使用引用格式取回视图对象,可能我错过了。我想你真的很困惑,没有很好地描述你想要的。谢谢。你的味觉真的很好。如何获取textView对象?我不认为TypedArray有这样的方法。它们是getSring、getColor和其他,但不是GetView“ColorOptions view”的工作原理与“findViewById”可以访问的其他视图对象类似
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="my_widget">
<attr name="maxValueFromTextId" format="reference"/>
</declare-styleable>
</resources>
<RelativeLayout
<TextView
android:id="@+id/text1"
/>
<Mywidget
maxValueFromTextId="@id/text1"
/>
/>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="ColorOptionsView">
<attr name="titleText" format="string" localization="suggested" />
<attr name="valueColor" format="color" />
</declare-styleable>
</resources>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
<!-- define new name space for your attributes -->
xmlns:custom="http://schemas.android.com/apk/res/com.vogella.android.view.compoundview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<!-- Assume that this is your new component. It uses your new attributes -->
<com.vogella.android.view.compoundview.ColorOptionsView
android:id="@+id/main_color_options"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
custom:titleText="Background color"
custom:valueColor="@android:color/holo_green_light"
/>
</LinearLayout>
public class MainActivity extends Activity {
@Override
protected void onCreate( final Bundle savedInstanceState ) {
super.onCreate( savedInstanceState );
this.setContentView( R.layout.activity_main );
final ColorOptionsView colorOptionsView = ( ColorOptionsView ) this.findViewById( R.id.main_color_options);
}
}