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>
我應該在哪裏放這個代碼? –
@DaniilOrekhov回答編輯。 –
完美無缺!謝謝:) ps。我會投票給你答案,但是我的名聲太低了,不好意思:D –