在android中,是否可以完全以编程方式从支持库创建GridLayout(即不编写相应的xml)?
我在一个片段(称为PromotionLayoutFragment)中创建我的GridLayout,如下所示:
ViewGroup fragmentView = (ViewGroup)getView();
GridLayout gridLayout = new GridLayout(fragmentView.getContext());
gridLayout.setColumnCount(2);
gridLayout.setRowCount(15);
// add views to grid ...
fragmentView.addView(gridLayout);
Run Code Online (Sandbox Code Playgroud)
这在我使用API级别14中的GridLayout类时工作正常,但是当我使用支持库(v7-r9)中的GridLayout时,我得到:
08-10 15:54:52.209: ERROR/AndroidRuntime(14687): FATAL EXCEPTION: main
java.lang.NoClassDefFoundError: android.support.v7.gridlayout.R$dimen
at android.support.v7.widget.GridLayout.<init>(GridLayout.java:255)
at android.support.v7.widget.GridLayout.<init>(GridLayout.java:274)
at android.support.v7.widget.GridLayout.<init>(GridLayout.java:282)
at net.link.redbutton.fragment.PromotionLayoutFragment.showPromotions(PromotionLayoutFragment.java:168)
at net.link.redbutton.fragment.PromotionLayoutFragment.onImageResult(PromotionLayoutFragment.java:222)
at net.link.redbutton.fragment.SchedulerResponseFragment$1.onReceiveResult(SchedulerResponseFragment.java:37)
at android.os.ResultReceiver$MyRunnable.run(ResultReceiver.java:43)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4441)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
Run Code Online (Sandbox Code Playgroud) 旋转设备时,我的应用中的片段不会重新添加到活动的视图中.如果我正确理解文档(以及SO上的类似问题),这应该是片段管理器处理(不必在我的清单中指定android:configChanges).
我的活动看起来像:
public class MainActivity extends SherlockFragmentActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null){
getSupportFragmentManager().beginTransaction()
.add(R.id.main_fragment_placeholder, new DashboardFragment(),"dashboard")
.commit();
}
}
}
Run Code Online (Sandbox Code Playgroud)
片段看起来像(为了清楚起见,省略了clicklisteners等):
public class DashboardFragment extends SherlockFragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_dashboard, container, false);
}
}
Run Code Online (Sandbox Code Playgroud)
布局文件看起来像(activity_main.xml):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/main.main">
<FrameLayout android:id="@+id/main_fragment_placeholder"
android:layout_height="fill_parent"
android:layout_weight="1"
android:layout_width="0px"
/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
和fragment_layout.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main.wrapper"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<my.layout.DashboardLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/main.dashboard">
<!-- some buttons here -->
</my.layout.DashboardLayout> …Run Code Online (Sandbox Code Playgroud) 我需要开发一个magento扩展,它将一些内容添加到特定页面,例如产品视图页面.更具体地说,在产品页面上,需要添加按钮/链接以将产品添加到第三方愿望清单站点.
现在我做了一些研究,并不完全清楚最好的方法是什么:
理想情况下,扩展的用户需要进行最小配置(或XML编辑),并且扩展必须与现有布局或用户完成的修改兼容.