2016-06-29 104 views
3

所以,我需要爲一個片段容器(它是一個RelativeLayout中的FrameLayout)設置左邊距,這樣它就可以按照我需要的方式進行縮放。因此,我想在java中這樣做(儘管剩下的視圖相關的東西是用xml完成​​的),但我不知道如何處理它。我應該爲我的片段創建一個新的廣義類嗎?或者我應該在活動課上做?如果是這樣,它是否必須在特定的地方?Android:如何以編程方式爲片段設置頁邊距?

這裏是我的活動類:

public class LoginActivity extends AppCompatActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_login); 

    if (findViewById(R.id.fragment_container) != null) { 
     if (savedInstanceState != null) 
      return; 


     AuthFragment firstFragment = new AuthFragment(); 
     firstFragment.setArguments(getIntent().getExtras()); 
    getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, firstFragment).commit(); 
    } 

} 

和活動佈局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/app_layout" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="@color/navy"> 

    <com.kupol24.app.view.MyImageView 
     android:id="@+id/image_view" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_alignParentLeft="true"/> 

    <FrameLayout 
     android:id="@+id/container_holder" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_alignParentLeft="true" 
     android:layout_marginLeft="@dimen/fragment_margin" 
     android:layout_centerVertical="true"> 

     <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:id="@+id/fragment_container" 
      android:layout_gravity="center" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"/> 

    </FrameLayout> 
</RelativeLayout> 

回答

3

也許這將是有用的,儘量邊距設置爲FrameLayout裏(或相對):

final FrameLayout frameLayout = (FrameLayout)findViewById(R.id.fragment_container); 

FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) frameLayout.getLayoutParams(); 
//left, top, right, bottom 
params.setMargins(10, 10, 10, 10); 
frameLayout.setLayoutParams(params); 

(編輯) 我不知道那樣做你的代碼,但我會這樣做:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_login); 

    final FrameLayout frameLayout = (FrameLayout) findViewById(R.id.fragment_container); 
    if ((frameLayout != null) && (savedInstanceState == null)) { 
     final AuthFragment firstFragment = new AuthFragment(); 
     firstFragment.setArguments(getIntent().getExtras()); 
     getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, firstFragment).commit(); 
     FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) frameLayout.getLayoutParams(); 
     //left, top, right, bottom 
     params.setMargins(10, 0, 0, 0); //setting margin left to 10px 
     frameLayout.setLayoutParams(params); 
    } 
} 
+0

我應該在哪裏放這個代碼? –

+0

@DaniilOrekhov回答編輯。 –

+1

完美無缺!謝謝:) ps。我會投票給你答案,但是我的名聲太低了,不好意思:D –

相關問題