我正在尝试为添加到线性布局的每个文本框添加边距,该布局位于scrollview中。不过我设置了很大的利润,它并没有反映在用户界面上。为什么?我已经在这个堆栈溢出本身上提到了很多东西。
import android.content.res.Resources;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutCompat;
import android.util.TypedValue;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import org.w3c.dom.Text;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
ScrollView mainScrollView;
LinearLayout scrollViewContainer;
ArrayList<String> stringArr;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainScrollView = (ScrollView) findViewById(R.id.mainScrollView);
scrollViewContainer = (LinearLayout)findViewById(R.id.scrollViewContainer);
stringArr = new ArrayList<String>();
String str1 = "In computer programming, a string is traditionally a sequence of characters, either as a literal constant or as some kind of variable. " +
"The latter may allow its elements to be mutated and the length changed, or it may be fixed (after creation).";
String str2 = "Initializes a newly created String object so that it represents the same sequence of characters as the argument; " +
"in other words, the newly created string is a copy of the argument string. Unless an explicit copy of original is needed," +
" use of this constructor is unnecessary since Strings are immutable.";
for(int ii = 0; ii < 10; ii++){
if(ii%2 == 0) {
stringArr.add(str1);
}else{
stringArr.add(str2);
}
}
Resources r = getResources();
float pxLeftMargin = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 20, r.getDisplayMetrics());
float pxTopMargin = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 20, r.getDisplayMetrics());
float pxRightMargin = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10, r.getDisplayMetrics());
float pxBottomMargin = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 20, r.getDisplayMetrics());
LinearLayoutCompat.LayoutParams textViewLayoutParams = new LinearLayoutCompat.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
for(int ii = 0; ii < stringArr.size(); ii++){
TextView textView = new TextView(this);
textView.setTextSize(12f);
textView.setText(stringArr.get(ii));
textView.setLayoutParams(textViewLayoutParams);
textViewLayoutParams.setMargins(10,30,10,30);
textView.requestLayout();
scrollViewContainer.addView(textView);
}
}
}
答案 0 :(得分:2)
您的问题在于使用
LinearLayoutCompat.LayoutParams textViewLayoutParams = new LinearLayoutCompat.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
改为使用:
LinearLayout.LayoutParams textViewLayoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
答案 1 :(得分:1)
textViewLayoutParams.setMargins(10,30,10,30);
textView.setLayoutParams(textViewLayoutParams);
这可能是问题...你正在编辑textViewLayoutParams ** AFTER **你将它设置为textView.setLayoutParams(textViewLayoutParams)......
答案 2 :(得分:0)
您正在使用LinearLayout,因此请尝试使用LinearLayout.LayoutParams
答案 3 :(得分:-2)
`<RelativeLayout 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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="#fff"
tools:context="com.example.sheheryar.quicktransport.Signin">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="false">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:layout_width="250dp"
android:layout_height="155dp"
android:id="@+id/imageView2"
android:background="@drawable/logo1"
android:layout_marginTop="10dp"
android:layout_gravity="center_horizontal" />
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40px">
<EditText
android:layout_width="fill_parent"
android:layout_height="40dp"
android:textSize="15dp"
android:inputType="textEmailAddress"
android:ems="10"
android:id="@+id/editTextemail"
android:hint="Enter email"/>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp">
<EditText
android:layout_width="fill_parent"
android:layout_height="40dp"
android:textSize="15dp"
android:inputType="textPassword"
android:ems="10"
android:id="@+id/editTextpassword"
android:hint="Password"/>
</android.support.design.widget.TextInputLayout>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Remember Password"
android:textColor="#6b6b6b"
android:id="@+id/cbRemberPassword"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:checked="false" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Sign in"
android:id="@+id/buttonsigin"
android:textColor="#ffffff"
android:onClick="userlogin"
android:background="@drawable/button_styles1"
android:layout_marginTop="20dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OR"
android:id="@+id/textView1"
android:textColor="#f99906"
android:textStyle="bold"
android:textSize="18dp"
android:layout_marginTop="10dp"
android:layout_gravity="center_horizontal" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Create New Account"
android:id="@+id/textView"
android:textColor="#6b6b6b"
android:textSize="18dp"
android:layout_marginTop="5dp"
android:layout_gravity="center_horizontal" />
</LinearLayout>
</ScrollView>
</RelativeLayout>`
签入XML文件。在资源&gt;布局。设置边距。 也许这会有所帮助。