安卓:如何在FrameLayout元素中绘制



我正在开发一个多片段应用程序,无法获得任何好的信息:屏幕顶部是一个选项卡栏,您可以在其中切换到不同的片段。下面是我正在处理的片段。我想在最底层绘制内容,并在上面添加一个带按钮的EditText。绘制的内容必须在点击按钮时进行更新。我现在的想法是在框架布局中添加一个内容元素并绘制到其中。框架布局:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout  
xmlns:android="http://schemas.android.com/apk/res/android"  
android:layout_width="fill_parent"  
android:layout_height="fill_parent"
android:background="@color/frag_color_bg"> 
<!-- Content element here -->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" 
android:layout_height="fill_parent"  
android:orientation="vertical">  
<EditText
android:id="@+id/frag_text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@color/frag_color_te_bg"
android:hint="@string/frag_srvhint"
android:padding="2dp"
android:textColor="@color/frag_text1"
android:textSize="12sp">
</EditText>  
<Button
android:id="@+id/frag_plotbtn"
android:background="@drawable/frag_blue_btn"
android:text="@string/frag_plotlabel"
style="@style/frag_btn"
android:layout_marginTop="0dp">
</Button>
</LinearLayout>  
</FrameLayout> 

这可能吗?将一个内容元素放在map.xml中(注释的位置),并用Java绘制?如果这是可能的,我必须把哪个内容元素放在那里?风景?你有如何做到这一点的示例代码吗?

如果这是不可能的,实现这一点的最佳方式是什么?

欢迎任何想法或代码示例。我是安卓系统的新手,几天来一直在寻找解决方案,但网上的样本并不那么清楚。。。

非常感谢您的时间和帮助!

类本身:

public MapFragment extends Fragment {
private final static String LOG_TAG = "Map";
private View mContentView;
private EditText mTxt;
private Activity mAct;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (container == null) {
return null;
}
mAct = (Activity) getActivity();
mContentView = (View) inflater.inflate(R.layout.map, container,
false);
try {
mTxt = (EditText) mContentView.findViewById(R.id.frag_text1);
mContentView.findViewById(R.id.frag_plotbtn).setOnClickListener(
myOnClickListener);
} catch (Exception e) {
Log.w(LOG_TAG, e);
}
return mContentView;
}
public OnClickListener myOnClickListener = new OnClickListener() {
public void onClick(View v) {
switch (v.getId()) {
// Plot button
case R.id.frag_plotbtn:
// Update drawing here;
break;
default:
break;
}
}
};

我会在视图顶部使用Canvas。查看这篇文章:http://developer.android.com/guide/topics/graphics/2d-graphics.html#draw-带帆布

如果你使用它,你会把你自己的扩展视图放在你的评论位置。您将创建自己版本的onDraw()方法,并在想要重绘时(即按下按钮时)调用invalidate()。将其添加到您的XML中,如下所示:

<com.mypackage.MyView
id="@+id/myView"
... />

希望能有所帮助!

最新更新