作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
因此,我需要为 fragment 容器(它是 RelativeLayout 中的 FrameLayout)设置左边距,以便它按我需要的方式缩放。因此,我想在 java 中执行此操作(尽管其余与 View 相关的内容是在 xml 中完成的),但我不确定如何处理它。我应该为我的 fragment 创建一个新的通用类吗?还是应该在 Activity 课上做?如果是,是否必须在特定位置?
这是我的 Activity 课:
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();
}
}
和 Activity 布局:
<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);
}
}
关于java - Android:如何以编程方式为 fragment 设置边距?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38101533/
在 Rails 中,您可以使用嵌套路由为 has_one 和 has_many 关系创建 RESTful 路由。可以在 Rails Guides 上找到示例 请问有没有什么好的方法可以为habtm关系
我是一名优秀的程序员,十分优秀!