繁体   English   中英

Android:如何以编程方式为片段设置边距?

[英]Android: How to set margin for a fragment programmatically?

因此,我需要为片段容器(它是RelativeLayout中的FrameLayout)设置左边距,以便它按照我需要的方式进行缩放。 因此,我想在java中执行此操作(尽管其余与视图相关的内容在xml中完成),但我不确定如何处理它。 我应该为我的片段创建一个新的通用类吗? 或者我应该在活动课程中这样做? 如果是这样,它必须在特定的地方吗?

这是我的活动课:

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>

也许这会很有用,尝试将边距设置为Framelayout(或Relative):

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);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM