【问题标题】:Orientation configuration for different layouts android不同布局android的方向配置
【发布时间】:2014-07-30 17:06:45
【问题描述】:

我的肖像和风景模式有两种布局。

在 potrait 中我有一个 ListView,在 Landscape 中我有一个 GridView。

布局/myxml.xml

 <ListView
    android:id="@+id/android:list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_above="@+id/getdata" 
     android:divider="#b5b5b5"
    android:dividerHeight="1dp"
    android:listSelector="@drawable/list_selector" />

layout-land/myxml.xml

 <com.etsy.android.grid.StaggeredGridView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/grid_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:item_margin="8dp"
app:column_count="3" />

我在我的类中扩展了这些布局,它扩展了一个片段。我想根据方向膨胀两个视图。

这就是我所做的:-

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState)
    {
        Configuration config;

        if(config.orientation == Configuration.ORIENTATION_PORTRAIT)
        {
        View vi = inflater.inflate(R.layout.myxml, container, false);
newsList = (ListView)vi.findViewById(android.R.id.list); //Error Here

 itemsAdapter = new LazyAdapter(myContext, getList );
 newsList.setAdapter(itemsAdapter);
        }
        else if(config.orientation ==Configuration.ORIENTATION_LANDSCAPE)
        {
            View vi = inflater.inflate(R.layout.myxml, container, false);
                       mGridView =(StaggeredGridView)vi.findViewById(R.id.grid_view);
                        stagAdaper = new StaggeredAdapter(myContext, android.R.layout.simple_list_item_1, getList);
           mGridView.setAdapter(stagAdaper);
        }
}

但我得到一个错误:-

     Caused by: java.lang.NullPointerException
06-09 10:44:34.369: E/AndroidRuntime(2680):     at mypackage.class(HomeFragment.java:220)

【问题讨论】:

    标签: android listview gridview orientation


    【解决方案1】:

    问题是Configuration config; 为空,因为您没有实例化它。

    判断您是landscape 还是portrait 模式的另一种方法是实例化StaggeredGridView 和ListView 并检查其中一个是否为空。

    示例:

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
    
        View vi = inflater.inflate(R.layout.myxml, container, false);
        newsList = (ListView) vi.findViewById(android.R.id.list); // Error Here
        mGridView = (StaggeredGridView) vi.findViewById(R.id.grid_view);
    
        if (mGridView == null) {
            itemsAdapter = new LazyAdapter(myContext, getList);
            newsList.setAdapter(itemsAdapter);
        } else {
            stagAdaper = new StaggeredAdapter(myContext,
                    android.R.layout.simple_list_item_1, getList);
            mGridView.setAdapter(stagAdaper);
        }
    
    }
    

    关于:

    以上代码的作用是检查mGridView 是否为空,如果是则您当前处于portrait 模式,否则您处于landscape 模式

    【讨论】:

      【解决方案2】:

      这里你没有启动配置变量。像这样更改代码:

      if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
      ..Your code for LANDSCAPE view ....
      }else if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT){
      ..Your code for PORTRAIT view .....
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-03-30
        • 2014-03-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多