2016-04-29 17 views
0

ファイルmylayout.xmlに次のレイアウトがあります。私は透明なテーマを持っている私の活動のためにsetContentView(R.layout.mylayoutを)やっているとき、私のレイアウトは、画面の中央に表示なっている。このレイアウトで表示方法画面上からの相対的なレイアウト内容

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:background="@color/yellow" 
    android:layout_marginLeft="5dp" 
    android:layout_marginRight="5dp" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:padding="2dp" 
    android:id="@+id/Layout5" 
    android:layout_alignParentTop="true" 
    android:layout_gravity="top" 
    android:gravity="top" 
    android:orientation="vertical"> 

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:orientation="vertical" 
     android:layout_marginLeft="5dp" 
     android:layout_marginRight="5dp" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/Layout6" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:gravity="top"> 

    <ImageView 
     android:layout_marginLeft="5dp" 
     android:layout_marginRight="5dp" 
     android:background="@color/green" 
     android:layout_width="match_parent" 
     android:layout_height="200dp" 
     android:layout_marginTop="5dp" 
     android:id="@+id/imageView" 
     android:layout_gravity="center_horizontal" /> 

    <LinearLayout 
     android:id="@+id/linear_layout" 
     android:layout_width="match_parent" 
     android:layout_height="50dp" 
     android:orientation="horizontal" 
     android:layout_below="@+id/imageView"> 


     <Button 
      android:id="@+id/btn1" 
      android:layout_width="0dip" 
      android:layout_weight="1" 
      android:layout_height="match_parent" 
      android:text="@string/text_btn_1" 
      /> 

     <Button 
      android:id="@+id/btn2" 
      android:layout_width="0dip" 
      android:layout_weight="1" 
      android:layout_height="match_parent" 
      android:text="@string/text_btn_2" 
      /> 
     </LinearLayout> 
    </LinearLayout> 
</RelativeLayout> 

私は動的にこのレイアウトの配置を変更したいので、中央(デフォルト)と(次のコードを使用して)下の表示が働いています。

LinearLayout lLayout = (LinearLayout) findViewById(R.id.Layout6); 
    RelativeLayout.LayoutParams relativeParas = (RelativeLayout.LayoutParams) lLayout.getLayoutParams(); 
    relativeParas.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); 
    lLayout.setLayoutParams(relativeParas); 

しかし、何とか私はこのレイアウトを画面の上部に動的に表示する方法を理解できません。

ご協力いただければ幸いです。

+1

はアンドロイド作る –

+0

試しmatch_parentするlayout_height等しい "relativeParas.addRule(RelativeLayout.ALIGN_PARENT_TOPを);" – Nilabja

+0

トップからのコンテンツを表示するアクティビティのマニフェストにテーマを定義するだけです。これは、タイトルバーを削除し、画面全体をカバーします。 android:theme = "@アンドロイド:style/Theme.Black.NoTitleBar"、layout_widthとHeight属性をxmlファイルのmatch_parentまたはfill_parentに設定します。 – Tara

答えて

0

私はこの問題については、以下の修正プログラムを見つけました。 相対レイアウトの下に最後にフィラーライナーレイアウトを追加しました。

センターに表示するには何もしないでください。

次のコードを使用してください。フィラーのレイアウトを一番下に移動するだけです。

LinearLayout lLayout = (LinearLayout) findViewById(R.id.filler); 
    RelativeLayout.LayoutParams relativeParas = (RelativeLayout.LayoutParams) lLayout.getLayoutParams(); 
    relativeParas.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); 
    lLayout.setLayoutParams(relativeParas); 

次のコードを下に使用して表示します。レイアウト6を下に移動する。 layout_width&アンドロイド:

LinearLayout lLayout = (LinearLayout) findViewById(R.id.Layout6); 
    RelativeLayout.LayoutParams relativeParas = (RelativeLayout.LayoutParams) lLayout.getLayoutParams(); 
    relativeParas.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); 
    lLayout.setLayoutParams(relativeParas); 
0

は(下を揃えるために、それを設定する)以前に作られたルールを削除してください、そして新しいルールを追加しますremoveRuleは()以降API 17から利用可能であることを

//your code. 
LinearLayout lLayout = (LinearLayout) findViewById(R.id.Layout6); 
RelativeLayout.LayoutParams relativeParas = (RelativeLayout.LayoutParams) lLayout.getLayoutParams(); 
relativeParas.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); 
lLayout.setLayoutParams(relativeParas); 

//remove rule 
relativeParas.removeRule(RelativeLayout.ALIGN_PARENT_BOTTOM); 
relativeParas.addRule(RelativeLayout.ALIGN_PARENT_TOP); 
lLayout.setLayoutParams(relativeParas); 

は注意を。方法以下の

+0

これは機能しませんでした。私はすでにあなたの提案の後にそ​​れをやってみました。 – Gourav

+0

現在、レイアウトはどこに行くのですか? –

+0

コードを変更すると中央に残ります。 – Gourav

0

てみ修飾:

layout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:id="@+id/Layout5" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center" 
       android:layout_marginLeft="5dp" 
       android:layout_marginRight="5dp" 
       android:background="@color/yellow" 
       android:padding="2dp" 
       android:paddingLeft="5dp" 
       android:paddingRight="5dp"> 

    <ImageView 
     android:id="@+id/imageView" 
     android:layout_width="match_parent" 
     android:layout_height="200dp" 
     android:layout_gravity="center_horizontal" 
     android:layout_marginLeft="5dp" 
     android:layout_marginRight="5dp" 
     android:layout_marginTop="5dp" 
     android:background="@color/green"/> 

    <LinearLayout 
     android:id="@+id/linear_layout" 
     android:layout_width="match_parent" 
     android:layout_height="50dp" 
     android:layout_below="@+id/imageView" 
     android:orientation="horizontal"> 


     <Button 
      android:id="@+id/btn1" 
      android:layout_width="0dip" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:text="button1" 
      /> 

     <Button 
      android:id="@+id/btn2" 
      android:layout_width="0dip" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:text="button2" 
      /> 
    </LinearLayout> 
</RelativeLayout> 

クラス:

RelativeLayout Layout5 = (RelativeLayout)findViewById(R.id.Layout5); 
Layout5.setGravity(Gravity.BOTTOM); 
+0

画面の上半分に問題が表示されています。とにかく私は既にGravity Bottomを試しました。 – Gourav

関連する問題