android:在方向更改自定义视图,视图不变

时间:2017-06-27 08:32:33

标签: android xml android-layout

我的活动中有一个自定义视图元素和2个xml设计。 1表示横向模式,1表示横向模式,但当应用程序的方向更改时,活动更改为横向xml,但自定义视图保持其水平设计。 有人能帮我吗?。我必须实现自己的方式来更改配置更改或其他方面的布局吗?

public class NavigationDashBoard extends LinearLayout implements
        ITurnByTurnContract.DashboardView,
        TextToSpeechUtils.ITextToSpeechDelegate,
        IRouteContract.ViewNavigationPresenter,
        SVGPresenter.svgLoadDelegate{


    public NavigationDashBoard(Context context) {
        super(context);
        init(context,null);
    }

    public NavigationDashBoard(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        init(context, attributeSet);
    }


    public NavigationDashBoard(Context context, AttributeSet attributeSet, @AttrRes int defStyleAttr){
        super(context,attributeSet,defStyleAttr);
        init(context,attributeSet);
    }

    private void init(Context context, AttributeSet attributeSet){
        TypedArray a = context.obtainStyledAttributes(attributeSet, R.styleable.NavigationDashboardStyle);

        mVoiceEnabled = a.getBoolean(R.styleable.NavigationDashboardStyle_navigation_voice,DEFAULT_VOICE);
        mSpeedEnabled = a.getBoolean(R.styleable.NavigationDashboardStyle_navigation_show_speed, DEFAULT_SPEED);
        a.recycle();
        onCreate();
    }

    private void onCreate(){
        View v = LayoutInflater.from(getContext()).inflate(R.layout.route_navigation_dashboard_tool, this, true);

        mSmallFrame = (LinearLayout) v.findViewById(R.id.small_route_icon_frame);
        mSmallPoiIcon = (AppCompatImageView) v.findViewById(R.id.route_navigation_dashboard_route_poi_small);
        mSmallDirectionJunction = (TextView) v.findViewById(R.id.route_navigation_dashboard_route_junction_small);
        mSmallDirectionsIcon = (AppCompatImageView) v.findViewById(R.id.route_navigation_dashboard_route_direction_small);

        mdirections = (TextView) v.findViewById(R.id.route_navigation_dashboard_route_message);
        mdirections.setOnClickListener(new SpeakRepeatListener());
        mDirectionsIcon = (AppCompatImageView) v.findViewById(R.id.route_navigation_dashboard_route_direction);
        mDirectionJunction = (TextView) v.findViewById(R.id.route_navigation_dashboard_route_junction);
        mPoiIcon = (AppCompatImageView) v.findViewById(R.id.route_navigation_dashboard_route_poi);

        mAudio = (ImageView) v.findViewById(R.id.route_navigation_dashboard_volume);
        mAudio.setOnClickListener(new OnMute());

        mSvg = new SVGPresenter();

    }

2 个答案:

答案 0 :(得分:1)

我发现了我的问题,我不得不重写onConfigurationChanged

@Override
    protected void onConfigurationChanged(Configuration newConfig) {
            removeAllViews();
            onCreate();
    }

答案 1 :(得分:0)

没有足够的信息来帮助您,发布您的route_navigation_dashboard_tool.xml

但是从我现在看到你做错了,让我解释一下:

  1. 创建一个my_layout.xml(按照您的意愿调用它),在里面添加包含标签的自定义视图:

    <?xml version="1.0" encoding="utf-8"?>
    <com.example.yourpackage.NavigationDashBoard
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/my_nav_board"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"/>
    

    重要信息:您不需要Portrait的布局和Landscape的另一个布局,您只需创建一个,Android将使用它来显示两者。

    注意:为每个方向创建两个布局的唯一原因是,您计划在布局中显示完全不同的内容,例如更多数据或平板电脑的其他屏幕。

  2. 从您的活动setContentView(R.layout.my_layout);

  3. 加载此布局
  4. setContentView()之后,在您的活动的 onCreate()中找到您的自定义视图:

    NavigationDashBoard navBoard = (NavigationDashBoard) findViewById(R.id.my_nav_board);
    
  5. NavigationDashBoard.class 中修改您的方法,无需在其中自动扩充您的布局。

    private void onCreate(){
    
        mSmallFrame = (LinearLayout) findViewById(R.id.small_route_icon_frame);
        mSmallPoiIcon = (AppCompatImageView) findViewById(R.id.route_navigation_dashboard_route_poi_small);
        mSmallDirectionJunction = (TextView) findViewById(R.id.route_navigation_dashboard_route_junction_small);
        mSmallDirectionsIcon = (AppCompatImageView) findViewById(R.id.route_navigation_dashboard_route_direction_small);
    
        mdirections = (TextView) findViewById(R.id.route_navigation_dashboard_route_message);
        mdirections.setOnClickListener(new SpeakRepeatListener());
        mDirectionsIcon = (AppCompatImageView) findViewById(R.id.route_navigation_dashboard_route_direction);
        mDirectionJunction = (TextView) findViewById(R.id.route_navigation_dashboard_route_junction);
        mPoiIcon = (AppCompatImageView)     findViewById(R.id.route_navigation_dashboard_route_poi);
    
        mAudio = (ImageView) findViewById(R.id.route_navigation_dashboard_volume);
        mAudio.setOnClickListener(new OnMute());
    
        mSvg = new SVGPresenter();
    
    }
    
  6. 在找到navBoard后的Activity中,您需要找到自定义View的子项,以便调用您设计的方法:

    navBoard.onCreate()
    
  7. 您的应用应该准备就绪,视图将被活动每次屏幕方向更改重新创建(它将被销毁,然后再次创建),您现在也可以从活动中控制自定义视图,以便您可以设置重新创建后的数据。

  8. 建议:学会使用Fragments(来自支持库),我编写的步骤也会一样,唯一的变化就是你没有setContentView()而是膨胀一个视图: )

    希望这有帮助,祝你好运!