【问题标题】:Android: Can't align programmatically added edit text to top of screenAndroid:无法以编程方式对齐将edittext添加到屏幕顶部
【发布时间】:2016-03-09 01:53:06
【问题描述】:

嘿,所以我正在向我的片段添加自定义编辑文本,但由于某种原因,我无法让它与屏幕顶部对齐。它总是在屏幕中间结束。

“记事本”编辑文字:

public class LinedEditText extends EditText {
    private Rect mRect;
    private Paint mPaint;

    // we need this constructor for LayoutInflater
    public LinedEditText(Context context) {
        super(context);

        mRect = new Rect();
        mPaint = new Paint();
        mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        mPaint.setColor(getResources().getColor(R.color.textColorPrimary)); //SET YOUR OWN COLOR HERE
    }

    @Override
    protected void onDraw(Canvas canvas) {
        //int count = getLineCount();

        int height = getHeight();
        int line_height = getLineHeight();

        int count = height / line_height;

        if (getLineCount() > count)
            count = getLineCount();//for long text with scrolling

        Rect r = mRect;
        Paint paint = mPaint;
        int baseline = getLineBounds(0, r);//first line

        for (int i = 0; i < count; i++) {

            canvas.drawLine(r.left, baseline + 1, r.right, baseline + 1, paint);
            baseline += getLineHeight();//next line
        }

        super.onDraw(canvas);
    }
}

设置布局参数并添加到布局的代码:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View rootView = inflater.inflate(R.layout.fragment_note, container, false);
    LinedEditText linedEditText = new LinedEditText(getActivity());
    RelativeLayout relativeLayout = (RelativeLayout) rootView.findViewById(R.id.note_relative_layout);

    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
    params.alignWithParent = true;
    params.addRule(RelativeLayout.ALIGN_TOP);
    linedEditText.setLayoutParams(params);
    relativeLayout.addView(linedEditText);
    return rootView;}

XML:

<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"
tools:context="com.glenn.sacramenttalktool.NoteFragment"
android:id="@+id/note_relative_layout"
android:gravity="top">

我尝试过使用 FrameLayout、LinearLayout 和 RelativeLayout,但行为是相同的。我所需要的只是让编辑文本与屏幕顶部对齐。

主要活动布局

<?xml version="1.0" encoding="utf-8"?>

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/ThemeOverlay.ldsTalkTool">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="LDS Talk Tool"
        android:textSize="20sp"
        android:textStyle="bold"
        android:textColor="@color/textColorPrimary"
        android:id="@+id/toolbar_title" />
    </android.support.v7.widget.Toolbar>

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabMode="fixed"
        app:tabGravity="fill"/>
</android.support.design.widget.AppBarLayout>

<android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"  />

我已经尝试像这样将它添加到 XML 中,即使这会引发错误:

<com.glenn.[app_name].LinedEditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

【问题讨论】:

  • 您为什么要以编程方式而不只是在 XML 中创建它?似乎毫无意义。此外,此代码不会将其与屏幕顶部对齐,而是与相对布局的顶部对齐。充其量它会将它放在片段的顶部。这个片段是否在您的活动中的屏幕顶部?
  • @GabeSechan 我没有在 XML 中添加它,因为我尝试这样做时会引发错误。我假设它可以在常规活动中使用,但不会在片段中使用。是的,片段位于我活动的屏幕顶部。
  • 然后你做错了将它添加到你的 xml 中。向xml添加自定义视图应该没有问题
  • 另外,发布活动布局
  • 做到了。我还注意到有一个 fragment_main 布局,在 tools:context 中名称的最后一部分有 MainActivity$PlaceholderFragment。会不会有问题?

标签: android android-edittext layoutparams


【解决方案1】:

试试这个。变化:

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);

到:

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.MATCH_PARENT);

【讨论】:

  • 没用。它仍然在屏幕中间。
【解决方案2】:

原来我已经从 LinedEditText 类的构造函数中删除了 AttributeSet 参数。

public LinedEditText(Context context) {
    super(context);

根据Error inflating when extending a class,需要上下文和 AttributeSet 来扩充视图,这就是为什么当我尝试仅在 XML 中添加视图时它会引发错误。它应该是这样的:

public LinedEditText(Context context, AttributeSet attrs) {
    super(context, attrs);

一旦我修复了它,我就可以直接将它添加到 XML 并将重力设置为“开始”。它现在填满了屏幕。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-17
    • 1970-01-01
    • 1970-01-01
    • 2020-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多