如何通过编程更改“包含”的布局的边距

4

我有一个LinearLayout,里面包含另一个布局。类似于这样:

<LinearLayout
                    android:id="@+id/linear_layout"
                    android:layout_width="match_parent"
                    android:layout_height="20dp"
                    android:visibility="gone"
                    tools:visibility="visible">

                    <include
                        layout="@layout/new_layout"
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_marginBottom="4dp"
                        android:layout_marginLeft="4dp"
                        android:layout_marginStart="4dp"
                        android:layout_weight="1"/>

</LinearLayout>

现在我想要做的是,编程更改所包含布局(new_layout)的“marginBottom”。我该怎么办?
我尝试调用各种LayoutParams并尝试更改边距,但不确定这是否是正确的方法。非常感谢任何帮助。
谢谢
5个回答

1
尝试一下...使用你的id创建线性布局。
    LinearLayout ll =(LinearLayout) findViewById(R.id.linear_layout);

    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT, 20);

    layoutParams.setMargins(left,top,right,bottom);

您可以在setMargins中传递值。

如果您想以dp为单位输入值,请尝试此方法

public void setMargin(Context con,ViewGroup.LayoutParams params,int dp) {

    final float scale = con.getResources().getDisplayMetrics().density;
    // convert the DP into pixel
    int pixel =  (int)(dp * scale + 0.5f); 

    ViewGroup.MarginLayoutParams s =(ViewGroup.MarginLayoutParams)params;
    s.setMargins(pixel,pixel,pixel,pixel);

    yourView.setLayoutParams(params);
}

在这里,您可以传递布局参数


0

include.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:id="@+id/include_layout">

<ImageView android:layout_width="90dp"
    android:layout_height="90dp"
    android:src="@drawable/cross" />

</LinearLayout>

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal">

<include layout="@layout/include"/>

<TextView android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Hello"
    android:padding="10dp" />

</LinearLayout>

MainActivity.class

public class MainActivity extends AppCompatActivity {

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

    LinearLayout included=(LinearLayout)findViewById(R.id.include_layout);
    LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
    params.setMargins(Math.round(10), Math.round(10), Math.round(10), Math.round(10));

    //set margin...
    included.setLayoutParams(params);
}
}

0

您不应直接更改或修改<include>标记中的边距。该标记只是将布局资源添加到当前视图层次结构中,而不添加额外的父视图容器。

因此,您要做的实质上是在“包含的布局”的根视图容器上设置边距。也就是说,您应该直接调用findViewById()以获取根视图,并在程序中设置所有边距,由于包含视图的父视图是一个Linear-layout,所以您应该使用

   LinerLayout.LayoutParams params = (LinerLayout.LayoutParams) includedRootView.getLayoutParams();
   params.setMargins();
   includedViewRoot.setLayoutParams(params);` 

0

你也可以通过尺寸来更改它

res/values/dimens.xml    
res/values-small/dimens.xml    
res/values-normal/dimens.xml    
res/values-xlarge/dimens.xml

enter image description here


-1

我认为这段代码很有帮助...

include.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical" 
 android:gravity="center">

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_launcher" >
</ImageView>

</LinearLayout>

activity_main.xml

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<include
    android:id="@+id/include_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="4dp"
    android:layout_marginStart="4dp"
     layout="@layout/include" />

 </LinearLayout>

主活动

package com.example.stackdemo;
import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;


 public class MainActivity extends Activity {

 LinearLayout include_layout;

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

    include_layout = (LinearLayout) findViewById(R.id.include_layout);

    @SuppressWarnings("deprecation")
    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.FILL_PARENT,
            LinearLayout.LayoutParams.MATCH_PARENT);

    layoutParams.setMargins(0, 0, 0, 100);
    include_layout.setLayoutParams(layoutParams);
 }

  }

1
这也是我想的,不过我代码中有一个小区别,就是'include.xml'是一个RelativeLayout。当我尝试使用RelativeLayout获取LayoutParams时,它会给我ClassCastException。所以我把它换成了LinearLayout,但它又崩溃了,显示Cannot cast ViewGroup to LinearLayout。各种奇怪的错误 :/ - user2453055

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