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

5
所以,我需要为一个片段容器(即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>
1个回答

7
也许这个方法会有用,尝试将边距设置到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);
    }
}

1
完美地工作了!谢谢 :) 顺便说一句,我本打算给你的回答投票,但是我的声誉不够高,无法做到,抱歉 :D - Daniil Orekhov

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接