2017-06-27 8 views
0

私は自分のアクティビティと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(); 

    } 

答えて

0

私は私の問題を発見した、私は、あなたを助け、あなたのroute_navigation_dashboard_tool.xml

を投稿が、私は今あなたのやって見ているものとするonConfigurationChanged

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

このようにしないと、アクティビティによってビューが作成され、次にそれらが削除されて再び作成され、無駄であり、正しい方法で実行され、アプリはよりスムーズかつ良好に動作します。あなた:) –

+0

こんにちは、通常、あなたはアプリがビューを2回構築するということについては正解でしょう。しかし、私はコアの問題を発見しました。オリエンテーションの変更は、このアクティビティのマニフェストファイルでブロックされます。したがって、このアクティビティの変更は手動で行う必要があります。 –

0

ない十分な情報を上書きしなければなりませんでしたそれはすべて間違って、私に説明させてください:

  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"/> 
    

    にとって必須:あなたは一つだけを作成し、Androidが使用する、ポートレートや風景のための別のレイアウトを必要としませんそれは両方のために表示する。

    注:あなたの計画は、より多くのデータやタブレットのための別の画面のように、レイアウトで全く別のものを表示する場合各方向のための2つのレイアウトを作成するための唯一の理由です。

  2. ロードあなたの活動setContentView(R.layout.my_layout);

  3. からこのレイアウト)(setContentView後をあなたの活動ののonCreate()でカスタムビューを探す:

    NavigationDashBoard navBoard = (NavigationDashBoard) findViewById(R.id.my_nav_board); 
    
  4. にあなたの方法を変更します。 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(); 
    
    } 
    
  5. あなたはnavBoardを見つけた後、あなたはあなたがそれを行うために設計されたメソッドを呼び出して、カスタムビューの子を検索する必要があります、あなたのアプリが準備する必要があります

    navBoard.onCreate() 
    
  6. を画面の向きが変更されるたびにアクティビティによってビューが再作成されます(破棄され、再度作成されます)。また、アクティビティからカスタムビューを制御できるようになり、再作成後にデータを設定できます。

アドバイス:は、私が書かれた手順は、唯一の変更は、あなたがsetContentView()を持たないで同じ動作します(サポートライブラリからの)フラグメントを使用することを学ぶ、代わりにビューを膨らませる:)

願わくば、これは幸運です!

関連する問題