Android 在主布局中添加视图的子类
以下是主布局文件的一部分:Android 在主布局中添加视图的子类,android,android-layout,android-fragments,android-intent,Android,Android Layout,Android Fragments,Android Intent,以下是主布局文件的一部分: <FrameLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity = "center" > <view android:id="@+id/board" android:layout_height="match_parent"
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity = "center" >
<view
android:id="@+id/board"
android:layout_height="match_parent"
android:layout_width="match_parent"
class="com.drawshapes.DrawCircles"></view>
</FrameLayout>
它给了我一个编译错误。我不确定我是否正确地接近它。我应该看看碎片吗?或者可能添加另一个活动文件?还是布局文件?
还有一段来自谷歌的片段,开头是这样的:
<com.drawshapes.DrawCircles>
android. .....
</>
安卓
但是我不知道应该把它们放在哪里,它们应该放在一个资源文件中还是创建另一个布局文件中
谢谢你的帮助。试试这个
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center" >
<com.drawshapes.DrawCircles
android:id="@+id/board"
android:layout_height="match_parent"
android:layout_width="match_parent" />
</FrameLayout>
找到了答案:
实现要在布局XML文件中使用的视图子类时,请使用以下格式:
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center" >
<com.drawshapes.DrawCircles
android:id="@+id/board"
android:layout_height="match_parent"
android:layout_width="match_parent" />
</FrameLayout>
那个标签还是不起作用。错误指向setContentView(R.layout.main_活动);在主活动类中。你把
画圈类放在哪里?是库还是模块?我发现我做错了什么。事实证明,要在XML中添加视图的子类作为自定义视图,我们必须实现这个构造函数:publicmyview(上下文上下文,AttributeSet attrs)粘贴DrawCircle类和错误日志
// gets called when adding this sub-view class from XML
public DrawShapes(Context con, AttributeSet attrs) {
super(con, attrs);
Log.v("constructor", "context & attributes ");
myList.add(new Shape(120, 170, 21,"square"));
}